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 );
    }
};
相关推荐
Asuka_46_6 分钟前
leetcode 第133场双周赛 100333.统计逆序对的数目【计数dp/滚动数组/前缀和优化】
算法·leetcode·前缀和·动态规划·acm·逆序对
数据分析螺丝钉1 小时前
力扣第218题“天际线问题”
经验分享·python·算法·leetcode·面试
GoGolang1 小时前
golang出现panic: runtime error: index out of range [0] with length 0(创建n阶矩阵时)
leetcode
橘子味的小橙3 小时前
leetCode-hot100-动态规划专题
java·算法·leetcode·动态规划
__AtYou__3 小时前
Golang | Leetcode Golang题解之第206题反转链表
leetcode·golang·题解
音符犹如代码5 小时前
3099.力扣每日一题7/3 Java(击败100%)
java·算法·leetcode
Rstln6 小时前
【遍历链表】个人练习-Leetcode-LCR 029. 循环有序列表的插入
数据结构·leetcode·链表
山脚ice7 小时前
【CT】LeetCode手撕—94. 二叉树的中序遍历
算法·leetcode
weixin_452013059 小时前
[leetcode hot 150]第三题,无重复字符的最长子串
算法·leetcode
驱动男孩13 小时前
leetCode.93. 复原 IP 地址
leetcode·dfs