当前位置:网站首页>Preorder, inorder, follow-up of binary tree (non recursive version)
Preorder, inorder, follow-up of binary tree (non recursive version)
2022-07-01 15:38:00 【Try to be better zz】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
Preface
The pre order, middle order and post order of the non recursive version of the binary tree are also often tested in the interview , Must be proficient in !
Next, we will talk about how to print the first, middle and last sequence of binary tree non recursively .
One 、 Before the order
Ideas : Data structure stack is generally used to change recursion to non recursion , Print when the current node is out of the stack , And a right node is added to the right node first ,
Then add the left node , Because the antecedent root is about , The stack is in reverse order , So first enter the node .
void Print(TreeNode* root)
{
// Before the order
stack<TreeNode*> s = stack<TreeNode*>();// Create a stack
s.push(root);// Head node stack
The order of stacking is right pressing right , There is left pressure left
while (!s.empty())
{
TreeNode* cur = s.top();
s.pop();
cout << to_string(cur->val)+"" ;
if (cur->right)
{
s.push(cur->right);
}
if (cur->left)
{
s.push(cur->left);
}
}
}
Two 、 In the following order
Ideas : Different from the previous sequence , In the following sequence, we need two stacks to exchange , The result order of the first stack is root right left , Because the stack is in reverse order , We will s1 Put the data in the stack s2 You get left and right , Subsequent operations are similar to those in the previous sequence .
In the following order
stack<TreeNode*> s1 = stack<TreeNode*>();
stack<TreeNode*> s2 = stack<TreeNode*>();
s1.push(root);
while (!s1.empty())
{
TreeNode* cur = s1.top();
s1.pop();
s2.push(cur);
if (cur->left)
{
s1.push(cur->left);
}
if (cur->right)
{
s1.push(cur->right);
}
}
while (!s2.empty())
{
TreeNode* cur = s2.top();
s2.pop();
cout << cur->val << endl;
}
3、 ... and 、 Middle preface
The code is as follows :
Middle preface
stack<TreeNode*> t = stack<TreeNode*>();
TreeNode* cur = root;
while (!t.empty() || cur!=nullptr)
{
if (cur!= nullptr)
{
t.push(cur);
cur = cur->left;
}
else
{
cur = t.top();
t.pop();
cout << cur->val << endl;
cur = cur->right;
}
}
summary
边栏推荐
- 【ROS进阶篇】第五讲 ROS中的TF坐标变换
- S32K1xx 微控制器的硬件設計指南
- Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
- [stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
- Tableapi & SQL and Kafka message insertion in Flink
- SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
- ThinkPHP advanced
- 常见健身器材EN ISO 20957认证标准有哪些
- Zhang Chi's class: several types and differences of Six Sigma data
- Qt+pcl Chapter 9 point cloud reconstruction Series 2
猜你喜欢

Recommendation of data acquisition tools and detailed graphic process of data acquisition list

Survey of intrusion detection systems:techniques, datasets and challenges

The difference between arrow function and ordinary function in JS

Returning to the top of the list, the ID is still weak

Basic use process of cmake

雷神科技冲刺北交所,拟募集资金5.4亿元

Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results

MySQL审计插件介绍

华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设

【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
随机推荐
VIM from dislike to dependence (22) -- automatic completion
Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
《QT+PCL第六章》点云配准icp系列6
异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
go-zero实战demo(一)
Microservice tracking SQL (support Gorm query tracking under isto control)
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
[STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
Survey of intrusion detection systems:techniques, datasets and challenges
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
【ROS进阶篇】第五讲 ROS中的TF坐标变换
A unifying review of deep and shallow anomaly detection
【云动向】6月上云新风向!云商店热榜揭晓
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
入侵检测模型(An Intrusion-Detection Model)
《QT+PCL第六章》点云配准icp系列5
Wechat applet 02 - Implementation of rotation map and picture click jump