当前位置:网站首页>[LeetCode] 94. Inorder traversal of binary tree
[LeetCode] 94. Inorder traversal of binary tree
2022-08-02 02:46:00 【Cake cake ~】
题目
给定一个二叉树的根节点 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;
}
};
边栏推荐
- 线程的不同状态
- mysql 查看死锁
- Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
- Recursively check if a configuration item has changed and replace it
- 【LeetCode】20.有效的括号
- The state status is displayed incorrectly after the openGauss switch
- 欧拉公式的证明
- 很有意思的经历,很有意思的项目--文件夹对比工具
- The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
- 架构:微服务网关(SIA-Gateway)简介
猜你喜欢

使用docker安装mysql

四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料

接口测试神器Apifox究竟有多香?

The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
![[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline](/img/08/d693c7e2fff8343b55ff3c1f9317c6.jpg)
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline

Remember a gorm transaction and debug to solve mysql deadlock

Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products

第11章_数据库的设计规范

pyqt上手体验

Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
随机推荐
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
pyqt上手体验
【web】Understanding Cookie and Session Mechanism
analog IC layout-Parasitic effects
JVM调优实战
AcWing 1285. 单词 题解(AC自动机)
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
analog IC layout-Design for reliability
【web】理解 Cookie 和 Session 机制
OC和Swift语言的区别
考完PMP学什么?前方软考等着你~
OC中new和init的区别
How engineers treat open source
Install mysql using docker
淘宝详情.
永磁同步电机36问(三)——SVPWM代码实现
2022 NPDP take an examination of how the results?How to query?
架构:应用架构的演进以及微服务架构的落地实践
字符串常用方法
Safety (2)