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);
    }
};
相关推荐
不吃土豆的马铃薯8 分钟前
C++ 高性能网络缓冲区 Buffer 源码解析
linux·服务器·开发语言·网络·c++
.千余1 小时前
【C++】C++继承入门(下):友元、静态成员与菱形继承的底层逻辑
开发语言·c++·笔记·学习·其他
初中就开始混世的大魔王1 小时前
6 Fast DDS-传输层
开发语言·c++·中间件·信息与通信
代码中介商3 小时前
C++ 智能指针完全指南(三):weak_ptr 与循环引用
开发语言·c++
BestOrNothing_20153 小时前
ROS2 C++ 小车控制完整实战(二):自定义 msg 消息发布与订阅保姆级教程
c++·ros2·subscriber·publisher·msg·topic通信·自定义接口
-森屿安年-4 小时前
91. 解码方法
c++·动态规划
有点。4 小时前
C++(二分答案)
c++
程序喵大人4 小时前
【C++并发系列】第一章:多线程读写同一个变量为什么会出错
开发语言·c++·多线程·并发
梓䈑4 小时前
C++ 接入 SQLite 数据库:环境搭建、API 详解 与 两种执行方式对比
数据库·c++·sqlite
zh路西法5 小时前
基于yaml-cpp的C++参数服务器设计2:多级参数配置
linux·服务器·c++