当前位置:网站首页>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;
}
}
};
边栏推荐
- Remember aximp once Use of exe tool
- Redis official ORM framework is more elegant than redistemplate
- 100million single men and women "online dating", supporting 13billion IPOs
- php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
- C # realizes the communication between Modbus protocol and PLC
- 0-5VAC转4-20mA交流电流隔离变送器/转换模块
- Pyqt GUI interface and logic separation
- Unity development --- the mouse controls the camera to move, rotate and zoom
- Record a garbled code during servlet learning
- Install mxnet GPU version
猜你喜欢

Cannot find module 'xxx' or its corresponding type declaration

Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
Redis official ORM framework is more elegant than redistemplate

详解全志V853上的ARM A7和RISC-V E907之间的通信方式

Quick sort (diagram +c code)

微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹

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

Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda

Record a garbled code during servlet learning
随机推荐
Remember that a development is encountered in the pit of origin string sorting
微服务架构开源框架详情介绍
The whole network "chases" Zhong Xuegao
Ren Qian code compilation error modification
Matplotlib快速入门
戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
Attitude estimation (complementary filtering)
Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
Variables and constants
Understand the autograd package in pytorch
Gazebo import the mapping model created by blender
OpenGL jobs - shaders
Vs custom template - take the custom class template as an example
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
Loki, the "open source star picking program", realizes the efficient management of harbor logs
Use partial derivatives to display normals in unity
JS number is insufficient, and 0 is added
Px4 autonomous flight
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域