当前位置:网站首页>Leetcode94. Middle order traversal of binary trees
Leetcode94. Middle order traversal of binary trees
2022-07-07 22:49:00 【Qingshan's green shirt】
LeetCode94. Middle order traversal of binary trees
List of articles
1. problem

2. Ideas
(1) What is middle root traversal

(2) recursive
The same idea as before and after !
(3) iteration
It takes a stack !

a section ADL Pseudo code !
Process simulation 
3. Code implementation
(1) recursive
class Solution {
public:
void inOrder(TreeNode*cur,vector<int>& vec)
{
if(cur == NULL)return;
inOrder(cur->left,vec);// Left
vec.push_back(cur->val);// root
inOrder(cur->right,vec);// Right
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
inOrder(root, result);
return result;
}
};
(2) iteration
class Solution{
public:
vector<int> inorderTraversal(TreeNode* root){
vector<int> result;
stack<TreeNode*> st;// Create a stack
TreeNode* cur = root;
while(1)
{
while(cur != NULL){
// Every time you join one , First judge whether it can go down left , If there is one, put
st.push(cur);// Down the left branch , The visited node is put on the stack
cur = cur->left;
}
if(st.empty()) return result;// When the stack is empty, it returns !
cur = st.top();// If the last step cur->right Empty finger , Then here will continue to stack
st.pop();
result.push_back(cur->val);
cur = cur->right;
}
}
};
边栏推荐
- The whole network "chases" Zhong Xuegao
- [environment] pycharm sets the tool to convert QRC into py file
- Revit secondary development - project file to family file
- Matplotlib quick start
- 「开源摘星计划」Loki实现Harbor日志的高效管理
- Unity FAQ (I) lack of references
- Px4 autonomous flight
- Nx10.0 installation tutorial
- Force deduction - question 561 - array splitting I - step by step parsing
- 微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
猜你喜欢

100million single men and women "online dating", supporting 13billion IPOs

行测-图形推理-8-图群类

Amesim2016 and matlab2017b joint simulation environment construction

0-5vac to 4-20mA AC current isolated transmitter / conversion module

行测-图形推理-3-对称图形类

Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment

行测-图形推理-5-一笔画类

Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法

「开源摘星计划」Loki实现Harbor日志的高效管理

Vs custom template - take the custom class template as an example
随机推荐
Gazebo import the mapping model created by blender
Debezium系列之:源码阅读之SnapshotReader
Kaggle-Titanic
Debezium series: set role statement supporting mysql8
Revit secondary development - operation family documents
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
6-3 find the table length of the linked table
Ni9185 and ni9234 hardware settings in Ni Max
Redis cluster installation
How pyGame rotates pictures
Two methods of calling WCF service by C #
Debezium系列之: 支持在 KILL 命令中使用变量
OpenGL configure assimp
Visual design form QT designer design gui single form program
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
行测-图形推理-3-对称图形类
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
Typeorm automatically generates entity classes