当前位置:网站首页>二叉树的前序,中序,后续(非递归版本)
二叉树的前序,中序,后续(非递归版本)
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;
}
}
总结
边栏推荐
- 【ROS进阶篇】第五讲 ROS中的TF坐标变换
- 【天线】【3】CST一些快捷键
- 微服务追踪SQL(支持Isto管控下的gorm查询追踪)
- Implementation of deploying redis sentry in k8s
- Redis high availability principle
- Opencv learning note 4 -- bank card number recognition
- Shopping mall 6.27 to be completed
- [STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
- Tableapi & SQL and MySQL data query of Flink
- 《QT+PCL第六章》点云配准icp系列6
猜你喜欢

Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases

Lean Six Sigma project counseling: centralized counseling and point-to-point counseling

Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?

Guide de conception matérielle du microcontrôleur s32k1xx

Summary of point cloud reconstruction methods I (pcl-cgal)
![Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]](/img/ea/8c9f716717bc08f2e563c577738ec8.png)
Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]

【目标跟踪】|STARK

微信小程序03-文字一左一右显示,行内块元素居中

硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件

《性能之巅第2版》阅读笔记(五)--file-system监测
随机推荐
Returning to the top of the list, the ID is still weak
Reading notes of top performance version 2 (V) -- file system monitoring
点云重建方法汇总一(PCL-CGAL)
Phpcms background upload picture button cannot be clicked
[cloud trend] new wind direction in June! Cloud store hot list announced
【云动向】6月上云新风向!云商店热榜揭晓
【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
有些能力,是工作中学不来的,看看这篇超过90%同行
Create employee data in SAP s/4hana by importing CSV
Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
"Qt+pcl Chapter 6" point cloud registration ICP Series 6
skywalking 6.4 分布式链路跟踪 使用笔记
使用swiper制作手机端轮播图
Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
张驰课堂:六西格玛数据的几种类型与区别
ABAP-调用Restful API
Redis seckill demo
Sort out the four commonly used sorting functions in SQL