2022-11-22 17:47:26 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# 二叉树
|
|
|
|
|
|
|
|
|
|
「二叉树 Binary Tree」是一种非线性数据结构,代表着祖先与后代之间的派生关系,体现着 “一分为二” 的分治逻辑。类似于链表,二叉树也是以结点为单位存储的,结点包含「值」和两个「指针」。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```java title=""
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 链表结点类 */
|
|
|
|
|
class TreeNode {
|
|
|
|
|
int val; // 结点值
|
|
|
|
|
TreeNode left; // 左子结点指针
|
|
|
|
|
TreeNode right; // 右子结点指针
|
|
|
|
|
TreeNode(int x) { val = x; }
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```cpp title=""
|
2022-11-29 02:21:49 +08:00
|
|
|
|
/* 链表结点结构体 */
|
|
|
|
|
struct TreeNode {
|
|
|
|
|
int val; // 结点值
|
|
|
|
|
TreeNode *left; // 左子结点指针
|
|
|
|
|
TreeNode *right; // 右子结点指针
|
|
|
|
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```python title=""
|
2022-12-27 18:34:12 +08:00
|
|
|
|
""" 链表结点类 """
|
2022-11-29 02:21:49 +08:00
|
|
|
|
class TreeNode:
|
2022-12-20 15:52:00 +08:00
|
|
|
|
def __init__(self, val=None, left=None, right=None):
|
2022-11-29 02:21:49 +08:00
|
|
|
|
self.val = val # 结点值
|
|
|
|
|
self.left = left # 左子结点指针
|
|
|
|
|
self.right = right # 右子结点指针
|
2022-12-27 18:34:12 +08:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
val = self.val
|
|
|
|
|
left_node_val = self.left.val if self.left else None
|
|
|
|
|
right_node_val = self.right.val if self.right else None
|
|
|
|
|
return "<TreeNode: {}, leftTreeNode: {}, rightTreeNode: {}>".format(val, left_node_val, right_node_val)
|
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```go title=""
|
2022-12-03 20:25:24 +08:00
|
|
|
|
""" 链表结点类 """
|
|
|
|
|
type TreeNode struct {
|
|
|
|
|
Val int
|
|
|
|
|
Left *TreeNode
|
|
|
|
|
Right *TreeNode
|
|
|
|
|
}
|
|
|
|
|
""" 结点初始化方法 """
|
|
|
|
|
func NewTreeNode(v int) *TreeNode {
|
|
|
|
|
return &TreeNode{
|
|
|
|
|
Left: nil,
|
|
|
|
|
Right: nil,
|
|
|
|
|
Val: v,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title=""
|
2022-12-04 12:26:09 +08:00
|
|
|
|
/* 链表结点类 */
|
|
|
|
|
function TreeNode(val, left, right) {
|
2022-12-13 21:44:14 +08:00
|
|
|
|
this.val = (val === undefined ? 0 : val); // 结点值
|
|
|
|
|
this.left = (left === undefined ? null : left); // 左子结点指针
|
|
|
|
|
this.right = (right === undefined ? null : right); // 右子结点指针
|
2022-12-05 00:13:48 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title=""
|
2022-12-15 00:00:40 +08:00
|
|
|
|
/* 链表结点类 */
|
|
|
|
|
class TreeNode {
|
|
|
|
|
val: number;
|
|
|
|
|
left: TreeNode | null;
|
|
|
|
|
right: TreeNode | null;
|
|
|
|
|
|
|
|
|
|
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
|
|
|
|
this.val = val === undefined ? 0 : val; // 结点值
|
|
|
|
|
this.left = left === undefined ? null : left; // 左子结点指针
|
|
|
|
|
this.right = right === undefined ? null : right; // 右子结点指针
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title=""
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
结点的两个指针分别指向「左子结点 Left Child Node」和「右子结点 Right Child Node」,并且称该结点为两个子结点的「父结点 Parent Node」。给定二叉树某结点,将左子结点以下的树称为该结点的「左子树 Left Subtree」,右子树同理。
|
|
|
|
|
|
|
|
|
|
![binary_tree_definition](binary_tree.assets/binary_tree_definition.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 子结点与子树 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-26 13:24:35 +08:00
|
|
|
|
需要注意,父结点、子结点、子树是可以向下递推的。例如,如果将上图的「结点 2」看作父结点,那么其左子结点和右子结点分别为「结点 4」和「结点 5」,左子树和右子树分别为「结点 4 以下的树」和「结点 5 以下的树」。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
## 二叉树常见术语
|
|
|
|
|
|
2022-12-18 15:42:06 +08:00
|
|
|
|
二叉树的术语较多,建议尽量理解并记住。后续可能遗忘,可以在需要使用时回来查看确认。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-18 15:42:06 +08:00
|
|
|
|
- 「根结点 Root Node」:二叉树最顶层的结点,其没有父结点;
|
|
|
|
|
- 「叶结点 Leaf Node」:没有子结点的结点,其两个指针都指向 $\text{null}$ ;
|
|
|
|
|
- 结点所处「层 Level」:从顶置底依次增加,根结点所处层为 1 ;
|
|
|
|
|
- 结点「度 Degree」:结点的子结点数量,二叉树中度的范围是 0, 1, 2 ;
|
|
|
|
|
- 「边 Edge」:连接两个结点的边,即结点指针;
|
2022-12-18 15:59:34 +08:00
|
|
|
|
- 二叉树「高度」:二叉树中根结点到最远叶结点走过边的数量;
|
2022-12-18 15:42:06 +08:00
|
|
|
|
- 结点「深度 Depth」 :根结点到该结点走过边的数量;
|
|
|
|
|
- 结点「高度 Height」:最远叶结点到该结点走过边的数量;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
![binary_tree_terminology](binary_tree.assets/binary_tree_terminology.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 二叉树的常见术语 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-18 15:59:34 +08:00
|
|
|
|
!!! tip "高度与深度的定义"
|
|
|
|
|
|
|
|
|
|
值得注意,我们通常将「高度」和「深度」定义为“走过边的数量”,而有些题目或教材会将其定义为“走过结点的数量”,此时高度或深度都需要 + 1 。
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
## 二叉树最佳和最差结构
|
|
|
|
|
|
|
|
|
|
当二叉树的每层的结点都被填满时,达到「完美二叉树」;而当所有结点都偏向一边时,二叉树退化为「链表」。
|
|
|
|
|
|
|
|
|
|
![binary_tree_corner_cases](binary_tree.assets/binary_tree_corner_cases.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 二叉树的最佳和最差结构 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-18 15:59:34 +08:00
|
|
|
|
如下表所示,在最佳和最差结构下,二叉树的叶结点数量、结点总数、高度等达到极大或极小值。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
2022-11-26 22:14:02 +08:00
|
|
|
|
| | 完美二叉树 | 链表 |
|
|
|
|
|
| ----------------------------- | ---------- | ---------- |
|
2022-12-18 15:59:34 +08:00
|
|
|
|
| 第 $i$ 层的结点数量 | $2^{i-1}$ | $1$ |
|
|
|
|
|
| 树的高度为 $h$ 时的叶结点数量 | $2^h$ | $1$ |
|
|
|
|
|
| 树的高度为 $h$ 时的结点总数 | $2^{h+1} - 1$ | $h + 1$ |
|
|
|
|
|
| 树的结点总数为 $n$ 时的高度 | $\log_2 (n+1) - 1$ | $n - 1$ |
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
## 二叉树基本操作
|
|
|
|
|
|
|
|
|
|
**初始化二叉树。** 与链表类似,先初始化结点,再构建引用指向(即指针)。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree.java"
|
|
|
|
|
// 初始化结点
|
|
|
|
|
TreeNode n1 = new TreeNode(1);
|
|
|
|
|
TreeNode n2 = new TreeNode(2);
|
|
|
|
|
TreeNode n3 = new TreeNode(3);
|
|
|
|
|
TreeNode n4 = new TreeNode(4);
|
|
|
|
|
TreeNode n5 = new TreeNode(5);
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
n1.right = n3;
|
|
|
|
|
n2.left = n4;
|
|
|
|
|
n2.right = n5;
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree.cpp"
|
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
TreeNode* n1 = new TreeNode(1);
|
|
|
|
|
TreeNode* n2 = new TreeNode(2);
|
|
|
|
|
TreeNode* n3 = new TreeNode(3);
|
|
|
|
|
TreeNode* n4 = new TreeNode(4);
|
|
|
|
|
TreeNode* n5 = new TreeNode(5);
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1->left = n2;
|
|
|
|
|
n1->right = n3;
|
|
|
|
|
n2->left = n4;
|
|
|
|
|
n2->right = n5;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree.py"
|
2022-12-20 15:52:00 +08:00
|
|
|
|
# 初始化二叉树
|
|
|
|
|
# 初始化节点
|
|
|
|
|
n1 = TreeNode(val=1)
|
|
|
|
|
n2 = TreeNode(val=2)
|
|
|
|
|
n3 = TreeNode(val=3)
|
|
|
|
|
n4 = TreeNode(val=4)
|
|
|
|
|
n5 = TreeNode(val=5)
|
|
|
|
|
# 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2
|
|
|
|
|
n1.right = n3
|
|
|
|
|
n2.left = n4
|
|
|
|
|
n2.right = n5
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
n1 := NewTreeNode(1)
|
|
|
|
|
n2 := NewTreeNode(2)
|
|
|
|
|
n3 := NewTreeNode(3)
|
|
|
|
|
n4 := NewTreeNode(4)
|
|
|
|
|
n5 := NewTreeNode(5)
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.Left = n2
|
|
|
|
|
n1.Right = n3
|
|
|
|
|
n2.Left = n4
|
|
|
|
|
n2.Right = n5
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title="binary_tree.js"
|
2022-12-04 12:26:09 +08:00
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
let n1 = new TreeNode(1),
|
2022-12-13 21:44:14 +08:00
|
|
|
|
n2 = new TreeNode(2),
|
|
|
|
|
n3 = new TreeNode(3),
|
|
|
|
|
n4 = new TreeNode(4),
|
|
|
|
|
n5 = new TreeNode(5);
|
2022-12-04 12:26:09 +08:00
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
n1.right = n3;
|
|
|
|
|
n2.left = n4;
|
|
|
|
|
n2.right = n5;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree.ts"
|
2022-12-15 00:00:40 +08:00
|
|
|
|
/* 初始化二叉树 */
|
|
|
|
|
// 初始化结点
|
|
|
|
|
let n1 = new TreeNode(1),
|
|
|
|
|
n2 = new TreeNode(2),
|
|
|
|
|
n3 = new TreeNode(3),
|
|
|
|
|
n4 = new TreeNode(4),
|
|
|
|
|
n5 = new TreeNode(5);
|
|
|
|
|
// 构建引用指向(即指针)
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
n1.right = n3;
|
|
|
|
|
n2.left = n4;
|
|
|
|
|
n2.right = n5;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree.c"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree.cs"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
**插入与删除结点。** 与链表类似,插入与删除结点都可以通过修改指针实现。
|
|
|
|
|
|
|
|
|
|
![binary_tree_add_remove](binary_tree.assets/binary_tree_add_remove.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 在二叉树中插入与删除结点 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree.java"
|
|
|
|
|
TreeNode P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.left = n2;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree.cpp"
|
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
TreeNode* P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1->left = P;
|
|
|
|
|
P->left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1->left = n2;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree.py"
|
2022-12-20 15:52:00 +08:00
|
|
|
|
# 插入与删除结点
|
|
|
|
|
p = TreeNode(0)
|
|
|
|
|
# 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = p
|
|
|
|
|
p.left = n2
|
|
|
|
|
# 删除节点 P
|
|
|
|
|
n1.left = n2
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
p := NewTreeNode(0)
|
|
|
|
|
n1.Left = p
|
|
|
|
|
p.Left = n2
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.Left = n2
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title="binary_tree.js"
|
2022-12-04 12:26:09 +08:00
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
let P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.left = n2;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree.ts"
|
2022-12-15 00:00:40 +08:00
|
|
|
|
/* 插入与删除结点 */
|
|
|
|
|
const P = new TreeNode(0);
|
|
|
|
|
// 在 n1 -> n2 中间插入结点 P
|
|
|
|
|
n1.left = P;
|
|
|
|
|
P.left = n2;
|
|
|
|
|
// 删除结点 P
|
|
|
|
|
n1.left = n2;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree.c"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree.cs"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
|
|
插入结点会改变二叉树的原有逻辑结构,删除结点往往意味着删除了该结点的所有子树。因此,二叉树中的插入与删除一般都是由一套操作配合完成的,这样才能实现有意义的操作。
|
|
|
|
|
|
|
|
|
|
## 二叉树遍历
|
|
|
|
|
|
|
|
|
|
非线性数据结构的遍历操作比线性数据结构更加复杂,往往需要使用搜索算法来实现。常见的二叉树遍历方式有层序遍历、前序遍历、中序遍历、后序遍历。
|
|
|
|
|
|
|
|
|
|
### 层序遍历
|
|
|
|
|
|
|
|
|
|
「层序遍历 Hierarchical-Order Traversal」从顶至底、一层一层地遍历二叉树,并在每层中按照从左到右的顺序访问结点。
|
|
|
|
|
|
|
|
|
|
层序遍历本质上是「广度优先搜索 Breadth-First Traversal」,其体现着一种 “一圈一圈向外” 的层进遍历方式。
|
|
|
|
|
|
|
|
|
|
![binary_tree_bfs](binary_tree.assets/binary_tree_bfs.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 二叉树的层序遍历 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
广度优先遍历一般借助「队列」来实现。队列的规则是 “先进先出” ,广度优先遍历的规则是 ”一层层平推“ ,两者背后的思想是一致的。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree_bfs.java"
|
|
|
|
|
/* 层序遍历 */
|
|
|
|
|
List<Integer> hierOrder(TreeNode root) {
|
|
|
|
|
// 初始化队列,加入根结点
|
|
|
|
|
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
List<Integer> list = new ArrayList<>();
|
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
|
TreeNode node = queue.poll(); // 队列出队
|
|
|
|
|
list.add(node.val); // 保存结点值
|
|
|
|
|
if (node.left != null)
|
|
|
|
|
queue.offer(node.left); // 左子结点入队
|
|
|
|
|
if (node.right != null)
|
|
|
|
|
queue.offer(node.right); // 右子结点入队
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree_bfs.cpp"
|
|
|
|
|
/* 层序遍历 */
|
|
|
|
|
vector<int> hierOrder(TreeNode* root) {
|
|
|
|
|
// 初始化队列,加入根结点
|
|
|
|
|
queue<TreeNode*> queue;
|
|
|
|
|
queue.push(root);
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
vector<int> vec;
|
|
|
|
|
while (!queue.empty()) {
|
|
|
|
|
TreeNode* node = queue.front();
|
|
|
|
|
queue.pop(); // 队列出队
|
|
|
|
|
vec.push_back(node->val); // 保存结点
|
2022-11-29 03:21:02 +08:00
|
|
|
|
if (node->left != nullptr)
|
2022-11-29 02:21:49 +08:00
|
|
|
|
queue.push(node->left); // 左子结点入队
|
2022-11-29 03:21:02 +08:00
|
|
|
|
if (node->right != nullptr)
|
2022-11-29 02:21:49 +08:00
|
|
|
|
queue.push(node->right); // 右子结点入队
|
|
|
|
|
}
|
|
|
|
|
return vec;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree_bfs.py"
|
2022-12-27 18:34:12 +08:00
|
|
|
|
""" 层序遍历 """
|
|
|
|
|
def hier_order(root):
|
2022-12-20 15:52:00 +08:00
|
|
|
|
# 初始化队列,加入根结点
|
|
|
|
|
queue = collections.deque()
|
|
|
|
|
queue.append(root)
|
|
|
|
|
# 初始化一个列表,用于保存遍历序列
|
|
|
|
|
result = []
|
|
|
|
|
while queue:
|
|
|
|
|
# 队列出队
|
|
|
|
|
node = queue.popleft()
|
|
|
|
|
# 保存节点值
|
|
|
|
|
result.append(node.val)
|
|
|
|
|
if node.left is not None:
|
|
|
|
|
# 左子结点入队
|
|
|
|
|
queue.append(node.left)
|
|
|
|
|
if node.right is not None:
|
|
|
|
|
# 右子结点入队
|
|
|
|
|
queue.append(node.right)
|
|
|
|
|
return result
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree_bfs.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 层序遍历 */
|
|
|
|
|
func levelOrder(root *TreeNode) []int {
|
|
|
|
|
// 初始化队列,加入根结点
|
|
|
|
|
queue := list.New()
|
|
|
|
|
queue.PushBack(root)
|
|
|
|
|
// 初始化一个切片,用于保存遍历序列
|
|
|
|
|
nums := make([]int, 0)
|
|
|
|
|
for queue.Len() > 0 {
|
|
|
|
|
// poll
|
|
|
|
|
node := queue.Remove(queue.Front()).(*TreeNode)
|
|
|
|
|
// 保存结点
|
|
|
|
|
nums = append(nums, node.Val)
|
|
|
|
|
if node.Left != nil {
|
|
|
|
|
// 左子结点入队
|
|
|
|
|
queue.PushBack(node.Left)
|
|
|
|
|
}
|
|
|
|
|
if node.Right != nil {
|
|
|
|
|
// 右子结点入队
|
|
|
|
|
queue.PushBack(node.Right)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nums
|
|
|
|
|
}
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title="binary_tree_bfs.js"
|
2022-12-04 12:26:09 +08:00
|
|
|
|
/* 层序遍历 */
|
|
|
|
|
function hierOrder(root) {
|
|
|
|
|
// 初始化队列,加入根结点
|
|
|
|
|
let queue = [root];
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
let list = [];
|
|
|
|
|
while (queue.length) {
|
|
|
|
|
let node = queue.shift(); // 队列出队
|
|
|
|
|
list.push(node.val); // 保存结点
|
|
|
|
|
if (node.left)
|
|
|
|
|
queue.push(node.left); // 左子结点入队
|
|
|
|
|
if (node.right)
|
|
|
|
|
queue.push(node.right); // 右子结点入队
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree_bfs.ts"
|
2022-12-15 00:00:40 +08:00
|
|
|
|
/* 层序遍历 */
|
|
|
|
|
function hierOrder(root: TreeNode | null): number[] {
|
|
|
|
|
// 初始化队列,加入根结点
|
|
|
|
|
const queue = [root];
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
const list: number[] = [];
|
|
|
|
|
while (queue.length) {
|
|
|
|
|
let node = queue.shift() as TreeNode; // 队列出队
|
|
|
|
|
list.push(node.val); // 保存结点
|
|
|
|
|
if (node.left) {
|
|
|
|
|
queue.push(node.left); // 左子结点入队
|
|
|
|
|
}
|
|
|
|
|
if (node.right) {
|
|
|
|
|
queue.push(node.right); // 右子结点入队
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree_bfs.c"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree_bfs.cs"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
### 前序、中序、后序遍历
|
|
|
|
|
|
|
|
|
|
相对地,前、中、后序遍历皆属于「深度优先遍历 Depth-First Traversal」,其体现着一种 “先走到尽头,再回头继续” 的回溯遍历方式。
|
|
|
|
|
|
|
|
|
|
如下图所示,左侧是深度优先遍历的的示意图,右上方是对应的递归实现代码。深度优先遍历就像是绕着整个二叉树的外围 “走” 一圈,走的过程中,在每个结点都会遇到三个位置,分别对应前序遍历、中序遍历、后序遍历。
|
|
|
|
|
|
|
|
|
|
![binary_tree_dfs](binary_tree.assets/binary_tree_dfs.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 二叉树的前 / 中 / 后序遍历 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
|
|
|
|
| 位置 | 含义 | 此处访问结点时对应 |
|
|
|
|
|
| ---------- | ------------------------------------ | ----------------------------- |
|
|
|
|
|
| 橙色圆圈处 | 刚进入此结点,即将访问该结点的左子树 | 前序遍历 Pre-Order Traversal |
|
|
|
|
|
| 蓝色圆圈处 | 已访问完左子树,即将访问右子树 | 中序遍历 In-Order Traversal |
|
|
|
|
|
| 紫色圆圈处 | 已访问完左子树和右子树,即将返回 | 后序遍历 Post-Order Traversal |
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree_dfs.java"
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
void preOrder(TreeNode root) {
|
|
|
|
|
if (root == null) return;
|
|
|
|
|
// 访问优先级:根结点 -> 左子树 -> 右子树
|
|
|
|
|
list.add(root.val);
|
|
|
|
|
preOrder(root.left);
|
|
|
|
|
preOrder(root.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
void inOrder(TreeNode root) {
|
|
|
|
|
if (root == null) return;
|
|
|
|
|
// 访问优先级:左子树 -> 根结点 -> 右子树
|
|
|
|
|
inOrder(root.left);
|
|
|
|
|
list.add(root.val);
|
|
|
|
|
inOrder(root.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
void postOrder(TreeNode root) {
|
|
|
|
|
if (root == null) return;
|
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根结点
|
|
|
|
|
postOrder(root.left);
|
|
|
|
|
postOrder(root.right);
|
|
|
|
|
list.add(root.val);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-29 02:21:49 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree_dfs.cpp"
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
void preOrder(TreeNode* root) {
|
|
|
|
|
if (root == nullptr) return;
|
|
|
|
|
// 访问优先级:根结点 -> 左子树 -> 右子树
|
|
|
|
|
vec.push_back(root->val);
|
|
|
|
|
preOrder(root->left);
|
|
|
|
|
preOrder(root->right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
void inOrder(TreeNode* root) {
|
|
|
|
|
if (root == nullptr) return;
|
|
|
|
|
// 访问优先级:左子树 -> 根结点 -> 右子树
|
|
|
|
|
inOrder(root->left);
|
|
|
|
|
vec.push_back(root->val);
|
|
|
|
|
inOrder(root->right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
void postOrder(TreeNode* root) {
|
|
|
|
|
if (root == nullptr) return;
|
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根结点
|
|
|
|
|
postOrder(root->left);
|
|
|
|
|
postOrder(root->right);
|
|
|
|
|
vec.push_back(root->val);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree_dfs.py"
|
2022-12-27 18:34:12 +08:00
|
|
|
|
""" 前序遍历二叉树 """
|
|
|
|
|
def pre_order(root: typing.Optional[TreeNode]):
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if root is None:
|
|
|
|
|
return
|
2022-12-27 18:34:12 +08:00
|
|
|
|
|
2022-12-20 15:52:00 +08:00
|
|
|
|
# 访问优先级:根结点 -> 左子树 -> 右子树
|
|
|
|
|
result.append(root.val)
|
2022-12-27 18:34:12 +08:00
|
|
|
|
pre_order(root=root.left)
|
|
|
|
|
pre_order(root=root.right)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
|
|
|
|
|
2022-12-27 18:34:12 +08:00
|
|
|
|
""" 中序遍历二叉树 """
|
|
|
|
|
def in_order(root: typing.Optional[TreeNode]):
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if root is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 访问优先级:左子树 -> 根结点 -> 右子树
|
2022-12-27 18:34:12 +08:00
|
|
|
|
in_order(root=root.left)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
result.append(root.val)
|
2022-12-27 18:34:12 +08:00
|
|
|
|
in_order(root=root.right)
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
2022-12-20 15:52:00 +08:00
|
|
|
|
|
2022-12-27 18:34:12 +08:00
|
|
|
|
""" 后序遍历二叉树 """
|
|
|
|
|
def post_order(root: typing.Optional[TreeNode]):
|
2022-12-20 15:52:00 +08:00
|
|
|
|
if root is None:
|
|
|
|
|
return
|
2022-12-27 18:34:12 +08:00
|
|
|
|
|
2022-12-20 15:52:00 +08:00
|
|
|
|
# 访问优先级:左子树 -> 右子树 -> 根结点
|
2022-12-27 18:34:12 +08:00
|
|
|
|
post_order(root=root.left)
|
|
|
|
|
post_order(root=root.right)
|
2022-12-20 15:52:00 +08:00
|
|
|
|
result.append(root.val)
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree_dfs.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
func preOrder(node *TreeNode) {
|
|
|
|
|
if node == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 访问优先级:根结点 -> 左子树 -> 右子树
|
|
|
|
|
nums = append(nums, node.Val)
|
|
|
|
|
preOrder(node.Left)
|
|
|
|
|
preOrder(node.Right)
|
|
|
|
|
}
|
2022-11-29 02:21:49 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
func inOrder(node *TreeNode) {
|
|
|
|
|
if node == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 访问优先级:左子树 -> 根结点 -> 右子树
|
|
|
|
|
inOrder(node.Left)
|
|
|
|
|
nums = append(nums, node.Val)
|
|
|
|
|
inOrder(node.Right)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
func postOrder(node *TreeNode) {
|
|
|
|
|
if node == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根结点
|
|
|
|
|
postOrder(node.Left)
|
|
|
|
|
postOrder(node.Right)
|
|
|
|
|
nums = append(nums, node.Val)
|
|
|
|
|
}
|
2022-11-29 02:21:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title="binary_tree_dfs.js"
|
2022-12-04 12:26:09 +08:00
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
function preOrder(root){
|
|
|
|
|
if (root === null) return;
|
|
|
|
|
// 访问优先级:根结点 -> 左子树 -> 右子树
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
preOrder(root.left);
|
|
|
|
|
preOrder(root.right);
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-04 12:26:09 +08:00
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
function inOrder(root) {
|
|
|
|
|
if (root === null) return;
|
|
|
|
|
// 访问优先级:左子树 -> 根结点 -> 右子树
|
|
|
|
|
inOrder(root.left);
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
inOrder(root.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
function postOrder(root) {
|
|
|
|
|
if (root === null) return;
|
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根结点
|
|
|
|
|
postOrder(root.left);
|
|
|
|
|
postOrder(root.right);
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree_dfs.ts"
|
2022-12-15 00:00:40 +08:00
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
function preOrder(root: TreeNode | null): void {
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 访问优先级:根结点 -> 左子树 -> 右子树
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
preOrder(root.left);
|
|
|
|
|
preOrder(root.right);
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-15 00:00:40 +08:00
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
function inOrder(root: TreeNode | null): void {
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 访问优先级:左子树 -> 根结点 -> 右子树
|
|
|
|
|
inOrder(root.left);
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
inOrder(root.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
function postOrder(root: TreeNode | null): void {
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根结点
|
|
|
|
|
postOrder(root.left);
|
|
|
|
|
postOrder(root.right);
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree_dfs.c"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree_dfs.cs"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
|
|
使用循环一样可以实现前、中、后序遍历,但代码相对繁琐,有兴趣的同学可以自行实现。
|