当前位置:网站首页>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;
}
}
};
边栏推荐
- How to judge whether the input content is "number"
- IP网络主动测评系统——X-Vision
- Remember aximp once Use of exe tool
- How to write an augmented matrix into TXT file
- OpenGL job - texture
- Leetcode1984. Minimum difference in student scores
- 0-5VAC转4-20mA交流电流隔离变送器/转换模块
- Redis cluster installation
- Revit secondary development - intercept project error / warning pop-up
- 如何选择合适的自动化测试工具?
猜你喜欢
100million single men and women "online dating", supporting 13billion IPOs
Ni9185 and ni9234 hardware settings in Ni Max
OpenGL configuration vs2019
[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster
Vs custom template - take the custom class template as an example
行测-图形推理-7-相异图形类
IP网络主动测评系统——X-Vision
行测-图形推理-5-一笔画类
Antd date component appears in English
php 获取图片信息的方法
随机推荐
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
Pdf document signature Guide
行测-图形推理-9-线条问题类
C development - interprocess communication - named pipeline
php 获取图片信息的方法
UWA Q & a collection
Vs custom template - take the custom class template as an example
OpenGL jobs - shaders
ASP. Net core introduction V
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Get the exact offset of the element
ASP.NET Core入门五
Digital transformation: five steps to promote enterprise progress
行测-图形推理-5-一笔画类
0-5VAC转4-20mA交流电流隔离变送器/转换模块
Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
Revit secondary development - cut view
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution