当前位置:网站首页>Non recursive traversal of binary tree
Non recursive traversal of binary tree
2022-06-11 18:28:00 【HHYX.】
First, middle and last traversal of binary tree
Preorder traversal of two tree
Title List : Preorder traversal of two tree
Title Description
Give you the root node of the binary tree root , Of its node value Before the order Traverse .
Topic analysis
Non recursive implementation of preorder traversal of binary tree , Here, you can define a stack to store to nodes . The order of preorder traversal is about the root , So according to the last in, first out principle of the stack , You can stack each node first , Then take out the top elements in turn , Stack them in turn , And traverse the nodes in the right subtree . The specific code implementation is as follows :
Code implementation
vector<int> preorderTraversal(TreeNode* root)
{
vector<int> ret;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur || !st.empty())
{
while (cur)
{
st.push(cur);
ret.push_back(cur->val);
cur = cur->left;
}
TreeNode* tmp = st.top();
st.pop();
cur = tmp->right;
}
return ret;
}

Middle order traversal of binary trees
Topic link : Middle order traversal of binary trees
Title Description
Given the root node of a binary tree root , return its Middle preface Traverse .
Topic analysis
The normal recursive implementation of the middle order traversal of the binary tree only needs to be implemented in the order of the left root and the right , Here we try to realize the middle order traversal of binary tree by non recursion . The middle order traversal of a binary tree is similar to the previous order , The difference is that a first in root node is a first in left node , Pay attention to vector It's just a matter of time , The code implementation is as follows :
Code implementation
vector<int> inorderTraversal(TreeNode* root)
{
vector<int> ret;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur || !st.empty())
{
while (cur)
{
st.push(cur);
cur = cur->left;
}
TreeNode* tmp = st.top();
ret.push_back(tmp->val);
st.pop();
cur = tmp->right;
}
return ret;
}

Postorder traversal of binary trees
Topic link : Postorder traversal of binary trees
Title Description
Give you the root node of a binary tree root , Returns the After the sequence traversal .
Topic analysis
Normally, it is very simple to use recursion to realize the post order traversal of binary tree , Just follow the order of left and right roots , However, if the recursion depth of this tree is too deep, it may cause stack overflow , This problem can be solved in a non recursive way . How to realize post order traversal of binary tree non recursively ? It is different from preorder and inorder traversal , He needs to access the left node and the right node before accessing the root , So here we need to make a mark , Whether the right subtree node of this node has been accessed , If so, you can traverse the root , If not already visited , You need to access the right subtree node first , The code implementation is as follows :
Code implementation
vector<int> postorderTraversal(TreeNode* root)
{
vector<int> ret;
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* prev = nullptr;
while (cur || !st.empty())// Enter the loop when neither is empty
{
while (cur)// When the node is not empty , The left subtree node is put on the stack
{
st.push(cur);
cur = cur->left;
}
TreeNode* tmp = st.top();
if (tmp->right == nullptr || tmp->right == prev)
{
ret.push_back(tmp->val);
prev = tmp;
st.pop();
}
else
{
cur = tmp->right;
}
}
return ret;
}

边栏推荐
- Easycwmp source code analysis
- ubuntu 安装psql以及运行相关命令
- 力扣32题最长有效括号
- Ubuntu installs PSQL and runs related commands
- 软件需求工程复习
- Sorted circular linked list
- EditText amount limit
- 全志科技T3開發板(4核ARM Cortex-A7)——MQTT通信協議案例
- Combination sum of 39 questions
- [c language] output the students within the specified score range with the structure
猜你喜欢

Force deduction 23 questions, merging K ascending linked lists

SISO Decoder for SPC (补充章节1)

Radiogroup dynamically add RadioButton

Monitoring loss functions using visdom
![[c language] output the students with the highest scores with a structure. There can be multiple highest scores](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[c language] output the students with the highest scores with a structure. There can be multiple highest scores
![[c language] output the average score and the student data below or equal to the average score with the structure](/img/c4/263301a22b61c86a3e0df6ad2596f1.png)
[c language] output the average score and the student data below or equal to the average score with the structure

求字符串中最大的 3 位相同数字

v-for循环遍历

信号的处理与捕捉

Easycwmp source code analysis
随机推荐
SISO decoder for a general (n,n-1) SPC code(補充章節3)
DC-DC自举电容(BOOT)几个问题
力扣31 下一个排列
LDPC 7 - 解码简单例子
高性能架构设计
互联网_业务分析概述
关于keil中,while循环条件不成立却无法跳出的问题
非递归实现二叉树的前、中、后序遍历
H.264概念
SQL error injection 1
SA token single sign on SSO mode 2 URL redirection propagation session example
Various poses for text modification using sed
软件测试技术复习
Surveillance des fonctions de perte avec visdom
PIL-Pillow图像处理【1】-安装与新建
Function and principle of key in V-for
Getting started with CTF
论高可用架构
Labelme for image data annotation
Modern application of LDAP directory server