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 );
    }
};
相关推荐
CoderYanger21 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
夏鹏今天学习了吗1 天前
【LeetCode热题100(72/100)】前 K 个高频元素
leetcode
墨染点香1 天前
LeetCode 刷题【172. 阶乘后的零】
算法·leetcode·职场和发展
做怪小疯子1 天前
LeetCode 热题 100——链表——反转链表
算法·leetcode·链表
做怪小疯子1 天前
LeetCode 热题 100——矩阵——旋转图像
算法·leetcode·矩阵
sin_hielo1 天前
leetcode 2435
数据结构·算法·leetcode
稚辉君.MCA_P8_Java1 天前
Gemini永久会员 Java动态规划
java·数据结构·leetcode·排序算法·动态规划
小白程序员成长日记1 天前
2025.11.23 力扣每日一题
算法·leetcode·职场和发展
smj2302_796826522 天前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
leoufung2 天前
LeetCode 92 反转链表 II 全流程详解
算法·leetcode·链表