94. 二叉树的中序遍历

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> inorderTraversal(TreeNode* root) {
        vector<int> in;
        inorder(root,in);
        return in;
    }

    void inorder(TreeNode* root,vector<int>& in)
    {
        if(root==nullptr)
            return;
        
        inorder(root->left,in);
        in.push_back(root->val);
        inorder(root->right,in);
    }
};
相关推荐
啊董dong22 分钟前
noi-2026年5月12号小测验
数据结构·c++·算法
咩咦1 小时前
C++学习笔记24:构造函数初始化列表
c++·学习笔记·类和对象·构造函数·初始化列表·const引用
计算机安禾1 小时前
【c++面向对象编程】第43篇:可变参数模板(C++11):优雅处理不定长参数
java·开发语言·c++
10岁的博客2 小时前
C++ 进制转换:通用 a 进制转 b 进制(2-36进制)题解
开发语言·c++
小贾要学习2 小时前
【Linux】基于自定义TCP协议的日期计算器
linux·网络·c++·网络协议·tcp/ip
YsyaaabB3 小时前
ACM 模式通用代码模板
java·c++·python·算法
我命由我123453 小时前
C++ - 面向对象 - 析构函数
android·c语言·开发语言·c++·visualstudio·visual studio·android runtime
代码村新手4 小时前
C++-多态
开发语言·c++
玖釉-4 小时前
旋转图像:从矩阵转置、镜像到坐标变换的系统理解
c++·windows·算法·图形渲染
咩咦5 小时前
C++学习笔记23:const 成员函数
c++·学习笔记·类和对象·const·this指针·const成员函数