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);
    }
};
相关推荐
wen__xvn1 小时前
每日一题洛谷B3865 [GESP202309 二级] 小杨的 X 字矩阵c++
c++·算法·矩阵
makabaka_T_T1 小时前
25寒假算法刷题 | Day1 | LeetCode 240. 搜索二维矩阵 II,148. 排序链表
数据结构·c++·算法·leetcode·链表·矩阵
学游戏开发的2 小时前
UE求职Demo开发日志#19 给物品找图标,实现装备增加属性,背包栏UI显示装备
c++·笔记·游戏引擎·unreal engine
c-c-developer3 小时前
C++ Primer 标准库类型string
开发语言·c++
宁静致远20213 小时前
Ubuntu下的Doxygen+VScode实现C/C++接口文档自动生成
c++·vscode·ubuntu
Bluesonli3 小时前
第 2 天:创建你的第一个 UE5 C++ 项目!
c++·学习·ue5·虚幻·虚幻引擎·unreal engine
比特在路上4 小时前
蓝桥杯之c++入门(四)【循环】
c++·职场和发展·蓝桥杯
pay顿4 小时前
C++基础day1
c++·学习·笔试
孤寂码农_defector4 小时前
C++【iostream】数据库的部分函数功能介绍
c++
Icomi_4 小时前
【PyTorch】7.自动微分模块:开启神经网络 “进化之门” 的魔法钥匙
c语言·c++·人工智能·pytorch·python·机器学习·计算机视觉