leetCode.94.94. 二叉树的中序遍历

leetCode.94.94. 二叉树的中序遍历

没什么好讲思路的,直接上代码

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> inorderTraversal(TreeNode* root) {
        dfs( root );
        return res;
    }

    void dfs( TreeNode * root ) {
        if ( !root ) return ;

        dfs( root->left );
        res.push_back(root->val);
        dfs( root->right );
    }
};
相关推荐
z2005093013 小时前
今日算法(二叉搜索树)
学习·leetcode
运筹vivo@1 天前
2657. 找到两个数组的前缀公共数组 | 难度:中等
算法·leetcode·职场和发展·哈希表
一条大祥脚1 天前
ABC459 贪心构造|树形DP|组合数学|贪心|单调栈|势能|前缀和
算法·深度优先
叶小鸡1 天前
小鸡玩算法-力扣HOT100-动态规划(下)
算法·leetcode·动态规划
毅炼1 天前
今日LeetCode 摸鱼打卡
java·算法·leetcode
m0_629494731 天前
LeetCode 热题 100-----28. 两数相加
数据结构·算法·leetcode·链表
菜菜的顾清寒1 天前
力扣HOT100(25)环形链表
算法·leetcode·链表
Controller-Inversion1 天前
76. 最小覆盖子串
java·算法·leetcode
_日拱一卒1 天前
LeetCode:437路径总和Ⅲ
算法·leetcode·职场和发展
世纪末的小黑1 天前
【LeetCode自用】LeetCode自用记录贴,题目一:两数之和
数据结构·算法·leetcode