2023-02-16 03:39:01 +08:00
|
|
|
|
# 二叉树遍历
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
从物理结构的角度来看,树是一种基于链表的数据结构,因此其遍历方式是通过指针逐个访问节点。然而,树是一种非线性数据结构,这使得遍历树比遍历链表更加复杂,需要借助搜索算法来实现。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
二叉树常见的遍历方式包括层序遍历、前序遍历、中序遍历和后序遍历等。
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 层序遍历
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
「层序遍历 Level-Order Traversal」从顶部到底部逐层遍历二叉树,并在每一层按照从左到右的顺序访问节点。
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
层序遍历本质上属于「广度优先搜索 Breadth-First Traversal」,它体现了一种“一圈一圈向外扩展”的逐层搜索方式。
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![二叉树的层序遍历](binary_tree_traversal.assets/binary_tree_bfs.png)
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-02-15 03:43:29 +08:00
|
|
|
|
### 算法实现
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
广度优先遍历通常借助「队列」来实现。队列遵循“先进先出”的规则,而广度优先遍历则遵循“逐层推进”的规则,两者背后的思想是一致的。
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree_bfs.java"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{binary_tree_bfs}-[func]{levelOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree_bfs.cpp"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{}-[func]{levelOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree_bfs.py"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{}-[func]{level_order}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree_bfs.go"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{}-[func]{levelOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="binary_tree_bfs.js"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{}-[func]{levelOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree_bfs.ts"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{}-[func]{levelOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree_bfs.c"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{}-[func]{levelOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree_bfs.cs"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{binary_tree_bfs}-[func]{levelOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_tree_bfs.swift"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{}-[func]{levelOrder}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_tree_bfs.zig"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
[class]{}-[func]{levelOrder}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-15 03:34:06 +08:00
|
|
|
|
### 复杂度分析
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
**时间复杂度**:所有节点被访问一次,使用 $O(n)$ 时间,其中 $n$ 为节点数量。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
**空间复杂度**:在最差情况下,即满二叉树时,遍历到最底层之前,队列中最多同时存在 $\frac{n + 1}{2}$ 个节点,占用 $O(n)$ 空间。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 前序、中序、后序遍历
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
相应地,前序、中序和后序遍历都属于「深度优先遍历 Depth-First Traversal」,它体现了一种“先走到尽头,再回溯继续”的遍历方式。
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
如下图所示,左侧是深度优先遍历的示意图,右上方是对应的递归实现代码。深度优先遍历就像是绕着整个二叉树的外围“走”一圈,在这个过程中,在每个节点都会遇到三个位置,分别对应前序遍历、中序遍历和后序遍历。
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![二叉搜索树的前、中、后序遍历](binary_tree_traversal.assets/binary_tree_dfs.png)
|
2022-12-21 17:19:39 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
| 位置 | 含义 | 此处访问节点时对应 |
|
2022-12-21 17:19:39 +08:00
|
|
|
|
| ---------- | ------------------------------------ | ----------------------------- |
|
2023-04-09 04:32:17 +08:00
|
|
|
|
| 橙色圆圈处 | 刚进入此节点,即将访问该节点的左子树 | 前序遍历 Pre-Order Traversal |
|
2022-12-21 17:19:39 +08:00
|
|
|
|
| 蓝色圆圈处 | 已访问完左子树,即将访问右子树 | 中序遍历 In-Order Traversal |
|
|
|
|
|
| 紫色圆圈处 | 已访问完左子树和右子树,即将返回 | 后序遍历 Post-Order Traversal |
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2023-02-15 03:43:29 +08:00
|
|
|
|
### 算法实现
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree_dfs.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{binary_tree_dfs}-[func]{preOrder}
|
|
|
|
|
|
|
|
|
|
[class]{binary_tree_dfs}-[func]{inOrder}
|
|
|
|
|
|
|
|
|
|
[class]{binary_tree_dfs}-[func]{postOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree_dfs.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{}-[func]{preOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{inOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{postOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree_dfs.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{}-[func]{pre_order}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{in_order}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{post_order}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree_dfs.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{}-[func]{preOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{inOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{postOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="binary_tree_dfs.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{preOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{inOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{postOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree_dfs.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{preOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{inOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{postOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree_dfs.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{}-[func]{preOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{inOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{postOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree_dfs.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{binary_tree_dfs}-[func]{preOrder}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{binary_tree_dfs}-[func]{inOrder}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{binary_tree_dfs}-[func]{postOrder}
|
2022-12-21 17:19:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_tree_dfs.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{preOrder}
|
2023-01-19 00:12:54 +08:00
|
|
|
|
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{inOrder}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{postOrder}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_tree_dfs.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{}-[func]{preOrder}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{}-[func]{inOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{postOrder}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-21 17:19:39 +08:00
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
|
|
使用循环一样可以实现前、中、后序遍历,但代码相对繁琐,有兴趣的同学可以自行实现。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
### 复杂度分析
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
**时间复杂度**:所有节点被访问一次,使用 $O(n)$ 时间,其中 $n$ 为节点数量。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
**空间复杂度**:在最差情况下,即树退化为链表时,递归深度达到 $n$ ,系统占用 $O(n)$ 栈帧空间。
|