2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
# 7.2. 二叉树遍历
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
从物理结构角度看,树是一种基于链表的数据结构,因此遍历方式也是通过指针(即引用)逐个遍历节点。同时,树还是一种非线性数据结构,这导致遍历树比遍历链表更加复杂,需要使用搜索算法来实现。
|
2023-02-15 03:42:40 +08:00
|
|
|
|
|
|
|
|
|
常见的二叉树遍历方式有层序遍历、前序遍历、中序遍历、后序遍历。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 7.2.1. 层序遍历
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
「层序遍历 Level-Order Traversal」从顶至底、一层一层地遍历二叉树,并在每层中按照从左到右的顺序访问节点。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
层序遍历本质上是「广度优先搜索 Breadth-First Traversal」,其体现着一种“一圈一圈向外”的层进遍历方式。
|
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![二叉树的层序遍历](binary_tree_traversal.assets/binary_tree_bfs.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 二叉树的层序遍历 </p>
|
|
|
|
|
|
2023-02-15 03:42:40 +08:00
|
|
|
|
### 算法实现
|
|
|
|
|
|
2023-04-06 22:04:07 +08:00
|
|
|
|
广度优先遍历一般借助「队列」来实现。队列的规则是“先进先出”,广度优先遍历的规则是“一层层平推”,两者背后的思想是一致的。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree_bfs.java"
|
|
|
|
|
/* 层序遍历 */
|
2023-02-15 03:42:40 +08:00
|
|
|
|
List<Integer> levelOrder(TreeNode root) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化队列,加入根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
List<Integer> list = new ArrayList<>();
|
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
|
TreeNode node = queue.poll(); // 队列出队
|
2023-04-09 04:34:58 +08:00
|
|
|
|
list.add(node.val); // 保存节点值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node.left != null)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.offer(node.left); // 左子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node.right != null)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.offer(node.right); // 右子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree_bfs.cpp"
|
|
|
|
|
/* 层序遍历 */
|
2023-02-15 03:42:40 +08:00
|
|
|
|
vector<int> levelOrder(TreeNode* root) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化队列,加入根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
queue<TreeNode*> queue;
|
|
|
|
|
queue.push(root);
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
vector<int> vec;
|
|
|
|
|
while (!queue.empty()) {
|
|
|
|
|
TreeNode* node = queue.front();
|
|
|
|
|
queue.pop(); // 队列出队
|
2023-04-09 04:34:58 +08:00
|
|
|
|
vec.push_back(node->val); // 保存节点值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node->left != nullptr)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.push(node->left); // 左子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node->right != nullptr)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.push(node->right); // 右子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
return vec;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree_bfs.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def level_order(root: TreeNode | None) -> list[int]:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""层序遍历"""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 初始化队列,加入根节点
|
2023-03-23 18:56:56 +08:00
|
|
|
|
queue: deque[TreeNode] = deque()
|
2023-02-08 15:20:18 +08:00
|
|
|
|
queue.append(root)
|
|
|
|
|
# 初始化一个列表,用于保存遍历序列
|
2023-03-23 18:56:56 +08:00
|
|
|
|
res: list[int] = []
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while queue:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
node: TreeNode = queue.popleft() # 队列出队
|
|
|
|
|
res.append(node.val) # 保存节点值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if node.left is not None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
queue.append(node.left) # 左子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if node.right is not None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
queue.append(node.right) # 右子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return res
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree_bfs.go"
|
|
|
|
|
/* 层序遍历 */
|
2023-02-15 03:42:40 +08:00
|
|
|
|
func levelOrder(root *TreeNode) []int {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化队列,加入根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
queue := list.New()
|
|
|
|
|
queue.PushBack(root)
|
|
|
|
|
// 初始化一个切片,用于保存遍历序列
|
|
|
|
|
nums := make([]int, 0)
|
|
|
|
|
for queue.Len() > 0 {
|
2023-03-13 22:40:57 +08:00
|
|
|
|
// 队列出队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node := queue.Remove(queue.Front()).(*TreeNode)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 保存节点值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
nums = append(nums, node.Val)
|
|
|
|
|
if node.Left != nil {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 左子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
queue.PushBack(node.Left)
|
|
|
|
|
}
|
|
|
|
|
if node.Right != nil {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 右子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
queue.PushBack(node.Right)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nums
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="binary_tree_bfs.js"
|
|
|
|
|
/* 层序遍历 */
|
2023-02-15 03:42:40 +08:00
|
|
|
|
function levelOrder(root) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化队列,加入根节点
|
2023-03-03 02:46:12 +08:00
|
|
|
|
const queue = [root];
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
2023-03-03 02:46:12 +08:00
|
|
|
|
const list = [];
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (queue.length) {
|
2023-02-08 16:47:52 +08:00
|
|
|
|
let node = queue.shift(); // 队列出队
|
2023-04-09 04:34:58 +08:00
|
|
|
|
list.push(node.val); // 保存节点值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node.left)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.push(node.left); // 左子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node.right)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.push(node.right); // 右子节点入队
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree_bfs.ts"
|
|
|
|
|
/* 层序遍历 */
|
2023-02-15 03:42:40 +08:00
|
|
|
|
function levelOrder(root: TreeNode | null): number[] {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化队列,加入根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const queue = [root];
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
const list: number[] = [];
|
|
|
|
|
while (queue.length) {
|
|
|
|
|
let node = queue.shift() as TreeNode; // 队列出队
|
2023-04-09 04:34:58 +08:00
|
|
|
|
list.push(node.val); // 保存节点值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node.left) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.push(node.left); // 左子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
if (node.right) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.push(node.right); // 右子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree_bfs.c"
|
2023-02-15 03:42:40 +08:00
|
|
|
|
[class]{}-[func]{levelOrder}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree_bfs.cs"
|
|
|
|
|
/* 层序遍历 */
|
2023-02-15 03:42:40 +08:00
|
|
|
|
List<int> levelOrder(TreeNode root)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化队列,加入根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
Queue<TreeNode> queue = new();
|
|
|
|
|
queue.Enqueue(root);
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
List<int> list = new();
|
|
|
|
|
while (queue.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
TreeNode node = queue.Dequeue(); // 队列出队
|
2023-04-09 04:34:58 +08:00
|
|
|
|
list.Add(node.val); // 保存节点值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node.left != null)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.Enqueue(node.left); // 左子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (node.right != null)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.Enqueue(node.right); // 右子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_tree_bfs.swift"
|
|
|
|
|
/* 层序遍历 */
|
2023-02-15 03:42:40 +08:00
|
|
|
|
func levelOrder(root: TreeNode) -> [Int] {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化队列,加入根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
var queue: [TreeNode] = [root]
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
var list: [Int] = []
|
|
|
|
|
while !queue.isEmpty {
|
|
|
|
|
let node = queue.removeFirst() // 队列出队
|
2023-04-09 04:34:58 +08:00
|
|
|
|
list.append(node.val) // 保存节点值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if let left = node.left {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.append(left) // 左子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
if let right = node.right {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.append(right) // 右子节点入队
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_tree_bfs.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 层序遍历
|
2023-02-15 03:42:40 +08:00
|
|
|
|
fn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化队列,加入根节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
const L = std.TailQueue(*inc.TreeNode(T));
|
|
|
|
|
var queue = L{};
|
|
|
|
|
var root_node = try mem_allocator.create(L.Node);
|
|
|
|
|
root_node.data = root;
|
|
|
|
|
queue.append(root_node);
|
|
|
|
|
// 初始化一个列表,用于保存遍历序列
|
|
|
|
|
var list = std.ArrayList(T).init(std.heap.page_allocator);
|
|
|
|
|
while (queue.len > 0) {
|
|
|
|
|
var queue_node = queue.popFirst().?; // 队列出队
|
|
|
|
|
var node = queue_node.data;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
try list.append(node.val); // 保存节点值
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (node.left != null) {
|
|
|
|
|
var tmp_node = try mem_allocator.create(L.Node);
|
|
|
|
|
tmp_node.data = node.left.?;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.append(tmp_node); // 左子节点入队
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
|
|
|
|
if (node.right != null) {
|
|
|
|
|
var tmp_node = try mem_allocator.create(L.Node);
|
|
|
|
|
tmp_node.data = node.right.?;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
queue.append(tmp_node); // 右子节点入队
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-15 03:42:40 +08:00
|
|
|
|
### 复杂度分析
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
**时间复杂度**:所有节点被访问一次,使用 $O(n)$ 时间,其中 $n$ 为节点数量。
|
2023-02-15 03:42:40 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
**空间复杂度**:当为满二叉树时达到最差情况,遍历到最底层前,队列中最多同时存在 $\frac{n + 1}{2}$ 个节点,使用 $O(n)$ 空间。
|
2023-02-15 03:42:40 +08:00
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 7.2.2. 前序、中序、后序遍历
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
相对地,前、中、后序遍历皆属于「深度优先遍历 Depth-First Traversal」,其体现着一种“先走到尽头,再回头继续”的回溯遍历方式。
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
如下图所示,左侧是深度优先遍历的的示意图,右上方是对应的递归实现代码。深度优先遍历就像是绕着整个二叉树的外围“走”一圈,走的过程中,在每个节点都会遇到三个位置,分别对应前序遍历、中序遍历、后序遍历。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![二叉搜索树的前、中、后序遍历](binary_tree_traversal.assets/binary_tree_dfs.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 二叉搜索树的前、中、后序遍历 </p>
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
| 位置 | 含义 | 此处访问节点时对应 |
|
2023-02-08 15:20:18 +08:00
|
|
|
|
| ---------- | ------------------------------------ | ----------------------------- |
|
2023-04-09 04:34:58 +08:00
|
|
|
|
| 橙色圆圈处 | 刚进入此节点,即将访问该节点的左子树 | 前序遍历 Pre-Order Traversal |
|
2023-02-08 15:20:18 +08:00
|
|
|
|
| 蓝色圆圈处 | 已访问完左子树,即将访问右子树 | 中序遍历 In-Order Traversal |
|
|
|
|
|
| 紫色圆圈处 | 已访问完左子树和右子树,即将返回 | 后序遍历 Post-Order Traversal |
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2023-02-15 03:42:40 +08:00
|
|
|
|
### 算法实现
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree_dfs.java"
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
void preOrder(TreeNode root) {
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
list.add(root.val);
|
|
|
|
|
preOrder(root.left);
|
|
|
|
|
preOrder(root.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
void inOrder(TreeNode root) {
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
inOrder(root.left);
|
|
|
|
|
list.add(root.val);
|
|
|
|
|
inOrder(root.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
void postOrder(TreeNode root) {
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
postOrder(root.left);
|
|
|
|
|
postOrder(root.right);
|
|
|
|
|
list.add(root.val);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree_dfs.cpp"
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
void preOrder(TreeNode* root) {
|
|
|
|
|
if (root == nullptr) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
vec.push_back(root->val);
|
|
|
|
|
preOrder(root->left);
|
|
|
|
|
preOrder(root->right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
void inOrder(TreeNode* root) {
|
|
|
|
|
if (root == nullptr) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
inOrder(root->left);
|
|
|
|
|
vec.push_back(root->val);
|
|
|
|
|
inOrder(root->right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
void postOrder(TreeNode* root) {
|
|
|
|
|
if (root == nullptr) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
postOrder(root->left);
|
|
|
|
|
postOrder(root->right);
|
|
|
|
|
vec.push_back(root->val);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree_dfs.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def pre_order(root: TreeNode | None) -> None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""前序遍历"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if root is None:
|
|
|
|
|
return
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
res.append(root.val)
|
|
|
|
|
pre_order(root=root.left)
|
|
|
|
|
pre_order(root=root.right)
|
|
|
|
|
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def in_order(root: TreeNode | None) -> None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""中序遍历"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if root is None:
|
|
|
|
|
return
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
in_order(root=root.left)
|
|
|
|
|
res.append(root.val)
|
|
|
|
|
in_order(root=root.right)
|
|
|
|
|
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def post_order(root: TreeNode | None) -> None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""后序遍历"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if root is None:
|
|
|
|
|
return
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
post_order(root=root.left)
|
|
|
|
|
post_order(root=root.right)
|
|
|
|
|
res.append(root.val)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree_dfs.go"
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
func preOrder(node *TreeNode) {
|
|
|
|
|
if node == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
nums = append(nums, node.Val)
|
|
|
|
|
preOrder(node.Left)
|
|
|
|
|
preOrder(node.Right)
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
func inOrder(node *TreeNode) {
|
|
|
|
|
if node == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
inOrder(node.Left)
|
|
|
|
|
nums = append(nums, node.Val)
|
|
|
|
|
inOrder(node.Right)
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
func postOrder(node *TreeNode) {
|
|
|
|
|
if node == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
postOrder(node.Left)
|
|
|
|
|
postOrder(node.Right)
|
|
|
|
|
nums = append(nums, node.Val)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="binary_tree_dfs.js"
|
|
|
|
|
/* 前序遍历 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
function preOrder(root) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (root === null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
list.push(root.val);
|
|
|
|
|
preOrder(root.left);
|
|
|
|
|
preOrder(root.right);
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
function inOrder(root) {
|
|
|
|
|
if (root === null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
inOrder(root.left);
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
inOrder(root.right);
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
function postOrder(root) {
|
|
|
|
|
if (root === null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
postOrder(root.left);
|
|
|
|
|
postOrder(root.right);
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree_dfs.ts"
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
function preOrder(root: TreeNode | null): void {
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
list.push(root.val);
|
|
|
|
|
preOrder(root.left);
|
|
|
|
|
preOrder(root.right);
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
function inOrder(root: TreeNode | null): void {
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
inOrder(root.left);
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
inOrder(root.right);
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
function postOrder(root: TreeNode | null): void {
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
postOrder(root.left);
|
|
|
|
|
postOrder(root.right);
|
|
|
|
|
list.push(root.val);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree_dfs.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{}-[func]{preOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{inOrder}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{postOrder}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree_dfs.cs"
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
void preOrder(TreeNode? root)
|
|
|
|
|
{
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
list.Add(root.val);
|
|
|
|
|
preOrder(root.left);
|
|
|
|
|
preOrder(root.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
void inOrder(TreeNode? root)
|
|
|
|
|
{
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
inOrder(root.left);
|
|
|
|
|
list.Add(root.val);
|
|
|
|
|
inOrder(root.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
void postOrder(TreeNode? root)
|
|
|
|
|
{
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
postOrder(root.left);
|
|
|
|
|
postOrder(root.right);
|
|
|
|
|
list.Add(root.val);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_tree_dfs.swift"
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
|
func preOrder(root: TreeNode?) {
|
|
|
|
|
guard let root = root else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
list.append(root.val)
|
|
|
|
|
preOrder(root: root.left)
|
|
|
|
|
preOrder(root: root.right)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
|
func inOrder(root: TreeNode?) {
|
|
|
|
|
guard let root = root else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
inOrder(root: root.left)
|
|
|
|
|
list.append(root.val)
|
|
|
|
|
inOrder(root: root.right)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
|
func postOrder(root: TreeNode?) {
|
|
|
|
|
guard let root = root else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
postOrder(root: root.left)
|
|
|
|
|
postOrder(root: root.right)
|
|
|
|
|
list.append(root.val)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_tree_dfs.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 前序遍历
|
|
|
|
|
fn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
2023-02-09 22:55:29 +08:00
|
|
|
|
try list.append(root.?.val);
|
|
|
|
|
try preOrder(T, root.?.left);
|
|
|
|
|
try preOrder(T, root.?.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 中序遍历
|
|
|
|
|
fn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
2023-02-09 22:55:29 +08:00
|
|
|
|
try inOrder(T, root.?.left);
|
|
|
|
|
try list.append(root.?.val);
|
|
|
|
|
try inOrder(T, root.?.right);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 后序遍历
|
|
|
|
|
fn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
|
|
|
|
|
if (root == null) return;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
try postOrder(T, root.?.left);
|
|
|
|
|
try postOrder(T, root.?.right);
|
|
|
|
|
try list.append(root.?.val);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
|
|
使用循环一样可以实现前、中、后序遍历,但代码相对繁琐,有兴趣的同学可以自行实现。
|
2023-02-15 03:42:40 +08:00
|
|
|
|
|
|
|
|
|
### 复杂度分析
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
**时间复杂度**:所有节点被访问一次,使用 $O(n)$ 时间,其中 $n$ 为节点数量。
|
2023-02-15 03:42:40 +08:00
|
|
|
|
|
|
|
|
|
**空间复杂度**:当树退化为链表时达到最差情况,递归深度达到 $n$ ,系统使用 $O(n)$ 栈帧空间。
|