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 );
    }
};
相关推荐
alphaTao21 小时前
LeetCode 每日一题 2026/7/20-2026/7/26
算法·leetcode
圣保罗的大教堂1 天前
leetcode 3514. 不同 XOR 三元组的数目 II 中等
leetcode
青山木1 天前
Hot 100 ---腐烂的橘子
java·数据结构·后端·算法·leetcode·广度优先
xqqxqxxq1 天前
LeetCode Hot100 双指针专项题解笔记
笔记·算法·leetcode
闪电悠米1 天前
力扣hot100-54.螺旋矩阵-模拟边界控制详解
算法·leetcode·矩阵
To_OC2 天前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
闪电悠米2 天前
力扣hot100-73.矩阵置零-标记数组详解
算法·leetcode·矩阵
c238562 天前
下篇:回溯与剪枝的「智慧寻宝人」——DFS 进阶与网格 / 图论应用
c++·深度优先·图论·剪枝
过期动态2 天前
【LeetCode 热题 100】找到字符串中所有字母异位词
java·数据结构·算法·leetcode·职场和发展·rabbitmq
Adios7943 天前
设置交集大小至少为2
数据结构·算法·leetcode