当前位置:网站首页>二叉树的前序,中序,后续(非递归版本)
二叉树的前序,中序,后续(非递归版本)
2022-07-01 15:36:00 【努力变好的zz】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
二叉树的非递归版本的前序中序后序在面试中也经常考,必须熟练掌握!
下面将会讲如何非递归打印二叉树的前中后序。
一、前序
思路:递归改非递归一般都要用到数据结构栈,当前节点出栈的时候打印,并有右节点先加入右节点,
后加入左节点,因为前序根左右,而栈是逆序,所以先入有节点。
void Print(TreeNode* root)
{
//前序
stack<TreeNode*> s = stack<TreeNode*>();//创建栈
s.push(root);//头结点入栈
入栈顺序有右压右,有左压左
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);
}
}
}
二、后序
思路:与前序不同,后序我们需要两个栈进行调换,第一个栈得出的结果顺序是根右左,因为栈逆序,我们将s1栈中的数据放入s2就得到了左右根,后续操作和前序相似。
后序
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;
}
三、中序
代码如下:
中序
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;
}
}
总结
边栏推荐
- Can I choose to open an account on Great Wall Securities? Is it safe?
- 软件测试的可持续发展,必须要学会敲代码?
- [300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
- skywalking 6.4 分布式链路跟踪 使用笔记
- Opencv learning note 4 -- bank card number recognition
- 关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
- SAP s/4hana: one code line, many choices
- An intrusion detection model
- Qt+pcl Chapter 9 point cloud reconstruction Series 2
- C#/VB.NET 合并PDF文档
猜你喜欢
Implementation of deploying redis sentry in k8s

Junda technology - wechat cloud monitoring scheme for multiple precision air conditioners

JS中箭头函数和普通函数的区别

【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》

张驰课堂:六西格玛数据的几种类型与区别

STM32F4-TFT-SPI时序逻辑分析仪调试记录

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

【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测

【一天学awk】条件与循环

雷神科技冲刺北交所,拟募集资金5.4亿元
随机推荐
张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
Wechat applet 03 - text is displayed from left to right, and the block elements in the line are centered
Opencv learning note 4 -- bank card number recognition
Junda technology indoor air environment monitoring terminal PM2.5, temperature and humidity TVOC and other multi parameter monitoring
《QT+PCL第六章》点云配准icp系列5
【ROS进阶篇】第五讲 ROS中的TF坐标变换
Tableapi & SQL and Kafka message acquisition of Flink example
VIM from dislike to dependence (22) -- automatic completion
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
Introduction to MySQL audit plug-in
Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
微信小程序03-文字一左一右显示,行内块元素居中
《QT+PCL第九章》点云重建系列2
Deep operator overloading (2)
【一天学awk】函数与自定义函数
Connect the ABAP on premises system to the central inspection system for custom code migration
Qt+pcl Chapter 6 point cloud registration ICP Series 2
Basic use process of cmake
异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
使用 csv 导入的方式在 SAP S/4HANA 里创建 employee 数据