当前位置:网站首页>二叉树的前序,中序,后续(非递归版本)
二叉树的前序,中序,后续(非递归版本)
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;
}
}
总结
边栏推荐
- 【OpenCV 例程200篇】216. 绘制多段线和多边形
- 张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
- Qt+pcl Chapter 9 point cloud reconstruction Series 2
- Description | Huawei cloud store "commodity recommendation list"
- 厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
- 远程办公经验?来一场自问自答的介绍吧~ | 社区征文
- Hardware design guide for s32k1xx microcontroller
- 【ROS进阶篇】第五讲 ROS中的TF坐标变换
- Connect the ABAP on premises system to the central inspection system for custom code migration
- [target tracking] |stark
猜你喜欢

STM32ADC模拟/数字转换详解

Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
SQL常用的四个排序函数梳理

MySQL advanced 4

Guide de conception matérielle du microcontrôleur s32k1xx

Qt+pcl Chapter 6 point cloud registration ICP Series 2

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

S32K1xx 微控制器的硬件设计指南
![[advanced ROS] lesson 5 TF coordinate transformation in ROS](/img/4d/ae7d477bf6928005e16f046d461dcb.png)
[advanced ROS] lesson 5 TF coordinate transformation in ROS

Qt+pcl Chapter 9 point cloud reconstruction Series 2
随机推荐
张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
Photoshop plug-in HDR (II) - script development PS plug-in
Guide de conception matérielle du microcontrôleur s32k1xx
微信小程序02-轮播图实现与图片点击跳转
How to realize clock signal frequency division?
Opencv learning note 4 -- bank card number recognition
《QT+PCL第六章》点云配准icp系列4
远程办公经验?来一场自问自答的介绍吧~ | 社区征文
使用swiper制作手机端轮播图
Can I choose to open an account on Great Wall Securities? Is it safe?
swiper 轮播图,最后一张图与第一张图无缝衔接
phpcms后台上传图片按钮无法点击
Connect the ABAP on premises system to the central inspection system for custom code migration
ThinkPHP进阶
Reading notes of top performance version 2 (V) -- file system monitoring
Implementation of deploying redis sentry in k8s
Phpcms background upload picture button cannot be clicked
【ROS进阶篇】第五讲 ROS中的TF坐标变换
Tableapi & SQL and Kafka message acquisition of Flink example
JS中箭头函数和普通函数的区别