更新 二叉树的统一迭代法 排版格式修复

This commit is contained in:
jinbudaily
2023-07-20 16:22:00 +08:00
parent a13bb82921
commit cc510357c1

View File

@ -5,10 +5,11 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
> 统一写法是一种什么感觉
# 二叉树的统一迭代法
> 统一写法是一种什么感觉
## 思路
此时我们在[二叉树一入递归深似海从此offer是路人](https://programmercarl.com/二叉树的递归遍历.html)中用递归的方式,实现了二叉树前中后序的遍历。
@ -28,7 +29,7 @@
如何标记呢,**就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。** 这种方法也可以叫做标记法。
## 迭代法中序遍历
### 迭代法中序遍历
中序遍历代码如下:(详细注释)
@ -71,7 +72,7 @@ public:
此时我们再来看前序遍历代码。
## 迭代法前序遍历
### 迭代法前序遍历
迭代法前序遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**)
@ -102,7 +103,7 @@ public:
};
```
## 迭代法后序遍历
### 迭代法后序遍历
后续遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**)
@ -143,15 +144,11 @@ public:
所以大家根据自己的个人喜好,对于二叉树的前中后序遍历,选择一种自己容易理解的递归和迭代法。
## 其他语言版本
# 其他语言版本
Java
### Java
迭代法前序遍历代码如下:
```java
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
@ -235,7 +232,7 @@ class Solution {
}
```
Python
### Python
迭代法前序遍历:
```python
@ -309,7 +306,8 @@ class Solution:
return result
```
Go
### Go
> 前序遍历统一迭代法
```GO
@ -442,7 +440,7 @@ func postorderTraversal(root *TreeNode) []int {
}
```
javaScript:
### JavaScript:
> 前序遍历统一迭代法
@ -522,7 +520,7 @@ var postorderTraversal = function(root, res = []) {
```
TypeScript
### TypeScript
```typescript
// 前序遍历(迭代法)
@ -591,7 +589,8 @@ function postorderTraversal(root: TreeNode | null): number[] {
return res;
};
```
Scala:
### Scala:
```scala
// 前序遍历
object Solution {
@ -667,7 +666,7 @@ object Solution {
}
```
rust:
### Rust:
```rust
impl Solution{
@ -747,3 +746,4 @@ impl Solution{
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>