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 );
    }
};
相关推荐
·云扬·6 小时前
【Leetcode hot 100】101.对称二叉树
算法·leetcode·职场和发展
睡不醒的kun11 小时前
leetcode算法刷题的第三十二天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
共享家952715 小时前
经典动态规划题解
算法·leetcode·动态规划
1白天的黑夜117 小时前
栈-844.比较含退格的字符串-力扣(LeetCode)
c++·leetcode·
Swift社区19 小时前
LeetCode 378 - 有序矩阵中第 K 小的元素
算法·leetcode·矩阵
墨染点香19 小时前
LeetCode 刷题【73. 矩阵置零】
算法·leetcode·矩阵
林木辛19 小时前
LeetCode热题 438.找到字符中所有字母异位词 (滑动窗口)
算法·leetcode
dragoooon3420 小时前
[优选算法专题二——NO.16最小覆盖子串]
c++·算法·leetcode·学习方法