当前位置:网站首页>【LeetCode】94.二叉树的中序遍历
【LeetCode】94.二叉树的中序遍历
2022-08-02 02:40:00 【酥酥~】
题目
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
示例 1:
输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1,2,3,4,5,6,7]
输出:[4,2,5,1,6,3,7]
提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
题解
深度优先遍历
递归方法
/** * 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:
void fun(TreeNode* node,vector<int> &result)
{
if(node->left)
fun(node->left,result);
result.emplace_back(node->val);
if(node->right)
fun(node->right,result);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
if(root)
fun(root,result);
return result;
}
};
迭代方法
/** * 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> result;
stack<TreeNode*> mystack;
TreeNode* node = root;
while(!mystack.empty() || node!=nullptr)
{
while(node!=nullptr)
{
if(node)
mystack.emplace(node);
node = node->left;
}
node = mystack.top();
mystack.pop();
result.emplace_back(node->val);
node = node->right;
}
return result;
}
};
边栏推荐
- The failure to create a role in Dahua Westward Journey has been solved
- 因为WiFi原因navicat 无法连接数据库Mysql
- 数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
- KICAD 小封装拉线卡顿问题 解决方法
- 字符串常用方法
- Can Youxuan database import wrongly be restored?
- 1688API
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
- 欧拉公式的证明
猜你喜欢

2022-08-01 mysql/stoonedb slow SQL-Q18 analysis

analog IC layout-Environmental noise

Chopper webshell feature analysis

记一个gorm初始化的坑

mockjs生成假数据的基本使用

一次SQL优化,数据库查询速度提升 60 倍

因为WiFi原因navicat 无法连接数据库Mysql

Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案

Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol

BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
随机推荐
53. 最小的k个数
analog IC layout-Parasitic effects
Remember a gorm transaction and debug to solve mysql deadlock
亲身经历过的面试题
[ORB_SLAM2] SetPose, UpdatePoseMatrices
Can Youxuan database import wrongly be restored?
面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
Ringtone 1161. Maximum In-Layer Elements and
PHP live source code to achieve simple barrage effect related code
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
Chopper webshell feature analysis
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
20. 用两个栈实现队列
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
【CNN记录】tensorflow slice和strided_slice
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
字符串常用方法