当前位置:网站首页>LeetCode144. Preorder traversal of binary tree
LeetCode144. Preorder traversal of binary tree
2022-07-07 22:49:00 【Qingshan's green shirt】
Preorder traversal of two tree
List of articles
1. problem

2. Ideas
1. First, let's introduce root first traversal — According to the root - Left son - The right son traverses the whole binary tree in order
2. Recursive method 
3. Iterative method
Stack is not empty , One node per stack , Put it into the return array ! After a node is out of the stack, its right and left children enter the stack ( In order !)
3. Code implementation
(1) recursive
class Solution {
public:
void PreOrder(TreeNode*cur,vector<int>& vec)
{
if(cur == NULL)return;
vec.push_back(cur->val);// root visit !
PreOrder(cur->left,vec);// Left
PreOrder(cur->right,vec);// Right
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
PreOrder(root, result);
return result;
}
};
(2) iteration ( Utilization stack )
// Put the linear stack of binary tree nodes
class stack{
public:
//1. initialization
void InitStack(){
top = 0;
}
//2. Binary tree pointer stack
void Push(TreeNode *p){
stack[top++] = p;
}
//3. Bomb stack
void Pop(){
top--;
}
//4. Sentenced to empty
bool Empty() {
if (top == 0)return true;
return false;
}
//5. Take the top element of the stack
TreeNode* Top( ){
return stack[top-1];
}
private:
int top;
TreeNode *stack[512];
};
// Iterative method
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack st;
st.InitStack();// initialization
vector<int> result;
if(root == NULL) return result;
st.Push(root);// The root node is in the stack !
while(!st.empty())// Stack is not empty
{
TreeNode* node = st.Top();// The pointer points to the top node of the stack // in
st.Pop();// Out of the stack
result.push_back(node->val);
if(node->right) st.Push(node->right);// The right son enters the stack !( Empty nodes do not stack )
if(node->left) st.Push(node->left);// Left son into the stack !( Empty nodes do not stack )
}
return result; // The stack is empty. , end
}
};
边栏推荐
- JS number is insufficient, and 0 is added
- Attitude estimation (complementary filtering)
- Leetcode1984. Minimum difference in student scores
- Common verification rules of form components -2 (continuously updating ~)
- Robot autonomous exploration series papers environment code
- vite Unrestricted file system access to
- 行测-图形推理-9-线条问题类
- Debezium系列之: 支持在 KILL 命令中使用变量
- De la famille debezium: SET ROLE statements supportant mysql8
- Remove the default background color of chrome input input box
猜你喜欢

Force deduction - question 561 - array splitting I - step by step parsing

Robot autonomous exploration series papers environment code

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

CTF练习

Gazebo import the mapping model created by blender
Apple further entered the financial sector through the 'virtual card' security function in IOS 16

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

IP网络主动测评系统——X-Vision

Micro service remote debug, nocalhost + rainbow micro service development second bullet

Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
随机推荐
Micro service remote debug, nocalhost + rainbow micro service development second bullet
PKPM 2020 software installation package download and installation tutorial
Record layoutrebuild Forcerebuildlayoutimmediate does not take effect
Revit secondary development - Hide occlusion elements
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
Debezium系列之:引入对 LATERAL 运算符的支持
Welcome to CSDN markdown editor
Antd date component appears in English
Aspose. Words merge cells
C # Development -- pit encountered in JS intermodulation
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
Px4 autonomous flight
7-18 simple simulation of banking business queue
行测-图形推理-5-一笔画类
Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
Debezium系列之:源码阅读之SnapshotReader
Debezium series: source code reading snapshot reader