当前位置:网站首页>二叉树的前序,中序,后续(非递归版本)
二叉树的前序,中序,后续(非递归版本)
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;
}
}
总结
边栏推荐
- Survey of intrusion detection systems:techniques, datasets and challenges
- Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
- 说明 | 华为云云商店「商品推荐榜」
- Redis high availability principle
- MySQL backup and restore single database and single table
- 智能运维实战:银行业务流程及单笔交易追踪
- STM32F4-TFT-SPI时序逻辑分析仪调试记录
- Opencv Learning Notes 6 -- image feature [harris+sift]+ feature matching
- 《QT+PCL第六章》点云配准icp系列3
- Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
猜你喜欢
Photoshop plug-in HDR (II) - script development PS plug-in
MySQL高级篇4
如何实现时钟信号分频?
6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
点云重建方法汇总一(PCL-CGAL)
JS中箭头函数和普通函数的区别
张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
随机推荐
Logical analysis of automatic decision of SAP CRM organization model
VIM from dislike to dependence (22) -- automatic completion
采集数据工具推荐,以及采集数据列表详细图解流程
Phpcms background upload picture button cannot be clicked
Tableapi & SQL and Kafka message acquisition of Flink example
Qt+pcl Chapter 9 point cloud reconstruction Series 2
C#/VB.NET 合并PDF文档
【OpenCV 例程200篇】216. 绘制多段线和多边形
Hardware design guide for s32k1xx microcontroller
Stm32f4-tft-spi timing logic analyzer commissioning record
Flink 系例 之 TableAPI & SQL 与 Kafka 消息获取
【一天学awk】函数与自定义函数
微信小程序03-文字一左一右显示,行内块元素居中
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Qt+pcl Chapter 6 point cloud registration ICP series 4
How to realize clock signal frequency division?
ThinkPHP进阶
Summary of week 22-06-26
Using swiper to make mobile phone rotation map
Zhang Chi's class: several types and differences of Six Sigma data