当前位置:网站首页>【leetcode】二叉树遍历迭代法的统一模板
【leetcode】二叉树遍历迭代法的统一模板
2022-04-22 10:27:00 【前端corner】
题目
leetcode上二叉树前中后序遍历对应的题目
[145. 二叉树的后序遍历](
思路

前面学习了二叉树三种遍历方式的递归和迭代写法:
但是迭代写法前序和后序相似,而中序则很不同。
在遍历二叉树时,我们先访问的是中间节点,而前序遍历刚好就是要先处理中间节点,访问和处理可以同时进行。后续遍历的代码和前序遍历一样,也是先访问中间节点并处理中间节点,只不过交换左右节点的入栈顺序并且最后翻转一下结果数组。
但是中序遍历要先处理的是左节点,但是访问时先访问的是中间节点,处理和访问不能同时进行。
迭代统一写法
- 用栈来保存访问过的节点,注意要保证出栈的顺序与遍历顺序一致。比如中序遍历是“左中右”,那么入栈的顺序应该为“右中左”,这样出栈时才是"左中右"。
- 我们要处理的节点都是中间节点,只不过三种遍历方式处理中间节点的顺序不一样。在中间节点入栈时我们要标记一下,用于判断该节点是中间节点。怎么标记呢?可以在中间节点入栈后再入栈一个空节点null。
- 首先判断根节点是否为空,不为空就入栈。
- 开始遍历时,让当前节点指向栈顶节点,分为2种情况:
- 栈顶节点不为空,那么我们要按顺序将该节点和它的左右节点入栈,这里就是三种遍历方式唯一不同的地方。
- 栈顶节点为空,说明遇到了可以处理的中间节点,将空节点弹出,然后弹出中间节点,并将它的值加入结果数组。
- 遍历结束的条件就是栈为空。
代码

- 统一模板
var preorderTraversal = function(root) {
let stack = []
let ans = []
if(root) stack.push(root)
while(stack.length){
// 当前指针指向栈顶节点
let cur = stack[stack.length - 1]
if(cur){
// 当前节点不为空
stack.pop()
// TODO:按照各自遍历顺序将节点入栈,入栈中间节点后要入栈一个空节点
//...
}else{
//当前节点为空
// 弹出空节点
stack.pop()
// 弹出中间节点并将它的值加入结果数组
ans.push(stack.pop().val)
}
}
return ans
};
如以上代码所示,三种遍历方式只有TODO区域的代码有所差异
- 前序遍历
// TODO:按照各自遍历顺序将节点入栈,入栈中间节点后要入栈一个空节点
if(cur.right) stack.push(cur.right)
if(cur.left) stack.push(cur.left)
stack.push(cur)
stack.push(null)
- 中序遍历
// TODO:按照各自遍历顺序将节点入栈,入栈中间节点后要入栈一个空节点
if(cur.right) stack.push(cur.right)
stack.push(cur)
stack.push(null)
if(cur.left) stack.push(cur.left)
- 后续遍历
// TODO:按照各自遍历顺序将节点入栈,入栈中间节点后要入栈一个空节点
stack.push(cur)
stack.push(null)
if(cur.right) stack.push(cur.right)
if(cur.left) stack.push(cur.left)
关注我的专栏,每天更新三道leetcode题解,一起变强!
版权声明
本文为[前端corner]所创,转载请带上原文链接,感谢
https://blog.csdn.net/laplacepoisson/article/details/124339109
边栏推荐
- SWOOLE高性能内存数据库的使用和配置教程
- "Notes" exchange of SRE operation and maintenance system transformation of a telecom company
- Share some unknown and super easy-to-use API functions in sklearn module
- Google Adsense suggests that the advertising capture tool is wrong, which may lead to reduced revenue. What should we do
- 基于容器的PaaS混合云的几种形式
- Golang time formatting
- golang time strings常用方法
- [untitled]
- 【HLS】可变帧率和固定帧率拉流
- Slim 2022 Outlook: cram istio's complexity into the smart black box
猜你喜欢

PCIE XDMA IP核介绍(附列表)-明德扬科教(mdy-edu.com)

安全远程访问+安全文件传输+终端仿真+远程管理丨上海道宁联合VanDyke为IT行业人员带来可靠的、易于配置的软件
![[issue 307] in terms of implementation principle, why is Nacos so strong?](/img/e0/61e837cd6cf3528252a721e9ec13a3.png)
[issue 307] in terms of implementation principle, why is Nacos so strong?

matlab2009安装教程

Film online ticket purchase system based on SSM

树形dp——P1122 最大子树和

MySQL最新版8.0.21安装配置教程~

MySql5.7.26安装

TC397 EVADC

基于PyQt5实现数据动态可视化
随机推荐
机器人系统设计-coppeliasim仿真
The computer cleans the C disk (system disk) and the disk compression (disk partition) and expansion (disk expansion).
A case of MySQL implicit conversion
API 网关的功能用途及实现方式
【HLS】可变帧率和固定帧率拉流
天梯22模拟 L3-2 拼题A打卡奖励 (30 分)
*CTF 2022 Reverse Writeup
数字化时代,企业运维面临现状及挑战分析解读
Laya Uncaught ReferenceError: spine is not defined
接口规范性测试标准规范—详细
TC397 MCMCAN
Symfony3.4 数据库反向生成entity 已解决
About the problem that the picture library of tpshop open source mall version 6.0 does not display pictures
企业级 Web 开发的挑战
MySQL最新版8.0.21安装配置教程~
Two ways to write Coriolis (understand 'fn.length' and 'FN. Tostring()')
[untitled]
来文章啦~分享压缩和解压文件【在线网站】
下一代web服务器Caddy —— 筑梦之路
checkbox的使用