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

边栏推荐
- Modern application of LDAP directory server
- [c language] output students' names and scores in descending order of scores with structures
- SA token single sign on SSO mode 2 URL redirection propagation session example
- Cryptology Summary
- [C语言]用结构体把平均分和低于等于平均分的学生数据输出
- 牛客刷题——不要二
- 学习使用LSTM和IMDB评论数据进行情感分析训练
- 最长严格递增子序列
- Reading summary of nacos2.x source code
- 【无标题】
猜你喜欢

TR-069协议介绍

非递归实现二叉树的前、中、后序遍历
![[c language] limit the number of searches and output the maximum value found in the number of internal searches](/img/e6/cbb8dd54b49ade453251a70c8455e8.png)
[c language] limit the number of searches and output the maximum value found in the number of internal searches

求字符串中最大的 3 位相同数字
![[C语言]用结构体把平均分和低于等于平均分的学生数据输出](/img/c4/263301a22b61c86a3e0df6ad2596f1.png)
[C语言]用结构体把平均分和低于等于平均分的学生数据输出

DC-DC自举电容(BOOT)几个问题

合并多棵二叉搜索树

Ubuntu installs PSQL and runs related commands

Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case
![[C语言]用结构体把输入的指定分数范围内的学生输出](/img/40/cbd7fe5aafbaeb6237e6d257455e5e.png)
[C语言]用结构体把输入的指定分数范围内的学生输出
随机推荐
力扣刷题——二叉树的最近公共祖先
MATLAB 保存imshow绘制图片到指定文件夹中的两种方法
合并多棵二叉搜索树
牛客刷题——把字符串转换成整数
JS实现全屏展示的具体方法
“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
MMA-Self-defining function
Use egg Js+mongodb simple implementation of curdapi
新项目 搭建环境方法
viso的常见操作
NR LDPC 打孔-punctured
BigDecimal基本使用与闭坑介绍
神经网络与深度学习-2- 机器学习简单示例-PyTorch
ISCSI详解(四)——ISCSI服务端配置实战
全志科技T3開發板(4核ARM Cortex-A7)——MQTT通信協議案例
使用Visdom对损失函数进行监控
Getting started with CTF
map和set
[golang] leetcode - 349 Intersection of two arrays (hash table)
Introduction to social engineering practice