mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
更新 二叉树的迭代遍历 排版格式修复
This commit is contained in:
@ -4,16 +4,17 @@
|
||||
</a>
|
||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
> 听说还可以用非递归的方式
|
||||
|
||||
# 二叉树的迭代遍历
|
||||
|
||||
《代码随想录》算法视频公开课:
|
||||
* [写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2)
|
||||
* [写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g)
|
||||
相信结合视频在看本篇题解,更有助于大家对本题的理解。
|
||||
## 算法公开课
|
||||
|
||||
[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):
|
||||
|
||||
> 听说还可以用非递归的方式
|
||||
* **[写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2)**
|
||||
* **[写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g)**
|
||||
**相信结合视频在看本篇题解,更有助于大家对本题的理解。**
|
||||
|
||||
看完本篇大家可以使用迭代法,再重新解决如下三道leetcode上的题目:
|
||||
|
||||
@ -21,13 +22,15 @@
|
||||
* [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/)
|
||||
* [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/)
|
||||
|
||||
## 思路
|
||||
|
||||
为什么可以用迭代法(非递归的方式)来实现二叉树的前后中序遍历呢?
|
||||
|
||||
我们在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中提到了,**递归的实现就是:每一次递归调用都会把函数的局部变量、参数值和返回地址等压入调用栈中**,然后递归返回的时候,从栈顶弹出上一次递归的各项参数,所以这就是递归为什么可以返回上一层位置的原因。
|
||||
|
||||
此时大家应该知道我们用栈也可以是实现二叉树的前后中序遍历了。
|
||||
|
||||
## 前序遍历(迭代法)
|
||||
### 前序遍历(迭代法)
|
||||
|
||||
我们先看一下前序遍历。
|
||||
|
||||
@ -69,7 +72,7 @@ public:
|
||||
|
||||
但接下来,**再用迭代法写中序遍历的时候,会发现套路又不一样了,目前的前序遍历的逻辑无法直接应用到中序遍历上。**
|
||||
|
||||
## 中序遍历(迭代法)
|
||||
### 中序遍历(迭代法)
|
||||
|
||||
为了解释清楚,我说明一下 刚刚在迭代的过程中,其实我们有两个操作:
|
||||
|
||||
@ -112,7 +115,7 @@ public:
|
||||
|
||||
```
|
||||
|
||||
## 后序遍历(迭代法)
|
||||
### 后序遍历(迭代法)
|
||||
|
||||
再来看后序遍历,先序遍历是中左右,后续遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图:
|
||||
|
||||
@ -142,7 +145,7 @@ public:
|
||||
|
||||
```
|
||||
|
||||
# 总结
|
||||
## 总结
|
||||
|
||||
此时我们用迭代法写出了二叉树的前后中序遍历,大家可以看出前序和中序是完全两种代码风格,并不像递归写法那样代码稍做调整,就可以实现前后中序。
|
||||
|
||||
@ -155,11 +158,9 @@ public:
|
||||
当然可以,这种写法,还不是很好理解,我们将在下一篇文章里重点讲解,敬请期待!
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
# 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```java
|
||||
// 前序遍历顺序:中-左-右,入栈顺序:中-右-左
|
||||
@ -233,10 +234,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
```python
|
||||
# 前序遍历-迭代-LC144_二叉树的前序遍历
|
||||
@ -281,7 +279,7 @@ class Solution:
|
||||
# 取栈顶元素右结点
|
||||
cur = cur.right
|
||||
return result
|
||||
```
|
||||
```
|
||||
```python
|
||||
|
||||
# 后序遍历-迭代-LC145_二叉树的后序遍历
|
||||
@ -303,10 +301,9 @@ class Solution:
|
||||
stack.append(node.right)
|
||||
# 将最终的数组翻转
|
||||
return result[::-1]
|
||||
```
|
||||
```
|
||||
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
> 迭代法前序遍历
|
||||
|
||||
@ -400,7 +397,7 @@ func inorderTraversal(root *TreeNode) []int {
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
### JavaScript:
|
||||
|
||||
```js
|
||||
|
||||
@ -464,7 +461,7 @@ var postorderTraversal = function(root, res = []) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
// 前序遍历(迭代法)
|
||||
@ -519,7 +516,7 @@ function postorderTraversal(root: TreeNode | null): number[] {
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
### Swift:
|
||||
|
||||
```swift
|
||||
// 前序遍历迭代法
|
||||
@ -578,7 +575,8 @@ func inorderTraversal(_ root: TreeNode?) -> [Int] {
|
||||
return result
|
||||
}
|
||||
```
|
||||
Scala:
|
||||
### Scala:
|
||||
|
||||
```scala
|
||||
// 前序遍历(迭代法)
|
||||
object Solution {
|
||||
@ -645,7 +643,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
|
Reference in New Issue
Block a user