当前位置:网站首页>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;
}

边栏推荐
- VIM common commands
- Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case
- General terms in security field
- SISO decoder for a general (n,n-1) SPC code(补充章节3)
- Several commands related to domain name
- SISO Decoder for Repetition(补充章节4)
- Two methods for matlab to save imshow drawing pictures to a specified folder
- 牛客刷题——合法括号序列判断
- 全志T3开发板(4核ARM Cortex-A7)——系统启动阶段LOGO显示详解
- 非递归实现二叉树的前、中、后序遍历
猜你喜欢

Use egg Js+mongodb simple implementation of curdapi

非递归实现二叉树的前、中、后序遍历

SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
![[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)](/img/92/03de54c9f08eae5bc920b04d2b8493.png)
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)

全志科技T3开发板(4核ARM Cortex-A7)——MQTT通信协议案例

Labelme for image data annotation
![[C语言]对一个数组的元素排序后平移元素](/img/5b/3e74fc40787d94f6d0ab93332140ba.png)
[C语言]对一个数组的元素排序后平移元素

ubuntu 安装psql以及运行相关命令

* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解

SISO Decoder for Repetition(补充章节4)
随机推荐
Force buckle 34 finds the first and last positions of elements in a sorted array
MMA-Self-defining function
ubuntu 安装psql以及运行相关命令
高并发架构设计
viso的常见操作
SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
学习使用LSTM和IMDB评论数据进行情感分析训练
牛客刷题——part6
H.264概念
[untitled]
最长严格递增子序列
JS实现全屏展示的具体方法
[golang] leetcode - 292 Nim games (Mathematics)
力扣23题,合并K个升序链表
HashSet collection
* Jetpack 笔记 使用DataBinding
力扣刷题——二叉树的层序遍历Ⅱ
HashSet集合存储学生对象并遍历
5分钟了解攻防演练中的红蓝紫
Reading summary of nacos2.x source code