当前位置:网站首页>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;
}
}
};
边栏推荐
- Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
- C # Development -- pit encountered in JS intermodulation
- PHP method of obtaining image information
- Redis集群安装
- Install mxnet GPU version
- How to judge whether the input content is "number"
- Attitude estimation (complementary filtering)
- Aspose. Word operation word document (I)
- [environment] pycharm sets the tool to convert QRC into py file
- OpenGL job - texture
猜你喜欢
详解全志V853上的ARM A7和RISC-V E907之间的通信方式
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Amesim2016 and matlab2017b joint simulation environment construction
Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
Common verification rules of form components -2 (continuously updating ~)
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Robot autonomous exploration DSVP: code parsing
Redis cluster installation
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
随机推荐
Get the week start time and week end time of the current date
IP网络主动测评系统——X-Vision
0-5VAC转4-20mA交流电流隔离变送器/转换模块
Nx10.0 installation tutorial
23. Merge K ascending linked lists -c language
Attitude estimation (complementary filtering)
The essence of analog Servlet
Cannot find module 'xxx' or its corresponding type declaration
Ren Qian code compilation error modification
Unity development --- the mouse controls the camera to move, rotate and zoom
Debezium系列之: 支持在 KILL 命令中使用变量
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Redis official ORM framework is more elegant than redistemplate
行测-图形推理-5-一笔画类
Blender exchange group, welcome to the water group ~
Leetcode206. Reverse linked list
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
UWA Q & a collection
Force deduction - question 561 - array splitting I - step by step parsing
Revit secondary development - cut view