当前位置:网站首页>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
}
};
边栏推荐
- Debezium系列之:源码阅读之BinlogReader
- 行测-图形推理-2-黑白格类
- 「开源摘星计划」Loki实现Harbor日志的高效管理
- Redis官方ORM框架比RedisTemplate更优雅
- Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
- Gazebo import the mapping model created by blender
- How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
- 微服务架构开源框架详情介绍
- Force deduction - question 561 - array splitting I - step by step parsing
- 全面掌控!打造智慧城市建设的“领导驾驶舱”
猜你喜欢
Nx10.0 installation tutorial
Cannot find module 'xxx' or its corresponding type declaration
Form组件常用校验规则-2(持续更新中~)
PHP method of obtaining image information
Firefox browser installation impression notes clipping
行测-图形推理-7-相异图形类
Micro service remote debug, nocalhost + rainbow micro service development second bullet
[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster
Robot autonomous exploration series papers environment code
Record a garbled code during servlet learning
随机推荐
Gazebo import the mapping model created by blender
行测-图形推理-3-对称图形类
VTOL in Px4_ att_ Control source code analysis [supplement]
Force deduction - question 561 - array splitting I - step by step parsing
“拧巴”的早教行业:万亿市场,难出巨头
Understand the autograd package in pytorch
如何选择合适的自动化测试工具?
[interview arrangement] 0211 game engine server
Build an "immune" barrier in the cloud to prepare your data
Visual design form QT designer design gui single form program
行测-图形推理-2-黑白格类
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
Matplotlib quick start
How to write an augmented matrix into TXT file
XMIND mind mapping software sharing
Failed to initialize rosdep after installing ROS
戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
Relationship between URL and URI
The essence of analog Servlet