当前位置:网站首页>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;
}
}
};
边栏推荐
- Revit secondary development - link file collision detection
- 微服务架构开源框架详情介绍
- Revit secondary development - shielding warning prompt window
- Debezium series: source code reading snapshot reader
- Typeorm automatically generates entity classes
- 行测-图形推理-8-图群类
- Relationship between URL and URI
- Aspose. Word operation word document (II)
- Debezium series: MySQL tombstone event
- [interview arrangement] 0211 game engine server
猜你喜欢

UWA Q & a collection
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域

新版代挂网站PHP源码+去除授权/支持燃鹅代抽

Remember aximp once Use of exe tool

Antd date component appears in English

Ni9185 and ni9234 hardware settings in Ni Max

行测-图形推理-6-相似图形类

Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853

Pdf document signature Guide

How to judge whether the input content is "number"
随机推荐
Pdf document signature Guide
「开源摘星计划」Loki实现Harbor日志的高效管理
Matplotlib快速入门
Vs custom template - take the custom class template as an example
Cannot find module 'xxx' or its corresponding type declaration
How to close eslint related rules
The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
详解全志V853上的ARM A7和RISC-V E907之间的通信方式
CTF练习
De la famille debezium: SET ROLE statements supportant mysql8
Xcode modifies the default background image of launchscreen and still displays the original image
Remember aximp once Use of exe tool
Ren Qian code compilation error modification
OpeGL personal notes - lights
Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
行测-图形推理-8-图群类
0-5vac to 4-20mA AC current isolated transmitter / conversion module
How to choose the appropriate automated testing tools?
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence