|
|
@ -10,7 +10,7 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
## 7.2.1. 层序遍历
|
|
|
|
## 7.2.1. 层序遍历
|
|
|
|
|
|
|
|
|
|
|
|
「层序遍历 Hierarchical-Order Traversal」从顶至底、一层一层地遍历二叉树,并在每层中按照从左到右的顺序访问结点。
|
|
|
|
「层序遍历 Level-Order Traversal」从顶至底、一层一层地遍历二叉树,并在每层中按照从左到右的顺序访问结点。
|
|
|
|
|
|
|
|
|
|
|
|
层序遍历本质上是「广度优先搜索 Breadth-First Traversal」,其体现着一种“一圈一圈向外”的层进遍历方式。
|
|
|
|
层序遍历本质上是「广度优先搜索 Breadth-First Traversal」,其体现着一种“一圈一圈向外”的层进遍历方式。
|
|
|
|
|
|
|
|
|
|
|
@ -18,68 +18,76 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 二叉树的层序遍历 </p>
|
|
|
|
<p align="center"> Fig. 二叉树的层序遍历 </p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 实现代码
|
|
|
|
|
|
|
|
|
|
|
|
广度优先遍历一般借助「队列」来实现。队列的规则是“先进先出”,广度优先遍历的规则是 ”一层层平推“ ,两者背后的思想是一致的。
|
|
|
|
广度优先遍历一般借助「队列」来实现。队列的规则是“先进先出”,广度优先遍历的规则是 ”一层层平推“ ,两者背后的思想是一致的。
|
|
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree_bfs.java"
|
|
|
|
```java title="binary_tree_bfs.java"
|
|
|
|
[class]{binary_tree_bfs}-[func]{hierOrder}
|
|
|
|
[class]{binary_tree_bfs}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_tree_bfs.cpp"
|
|
|
|
```cpp title="binary_tree_bfs.cpp"
|
|
|
|
[class]{}-[func]{hierOrder}
|
|
|
|
[class]{}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
|
|
```python title="binary_tree_bfs.py"
|
|
|
|
```python title="binary_tree_bfs.py"
|
|
|
|
[class]{}-[func]{hier_order}
|
|
|
|
[class]{}-[func]{level_order}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
|
|
```go title="binary_tree_bfs.go"
|
|
|
|
```go title="binary_tree_bfs.go"
|
|
|
|
[class]{}-[func]{hierOrder}
|
|
|
|
[class]{}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="binary_tree_bfs.js"
|
|
|
|
```javascript title="binary_tree_bfs.js"
|
|
|
|
[class]{}-[func]{hierOrder}
|
|
|
|
[class]{}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_tree_bfs.ts"
|
|
|
|
```typescript title="binary_tree_bfs.ts"
|
|
|
|
[class]{}-[func]{hierOrder}
|
|
|
|
[class]{}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
|
|
```c title="binary_tree_bfs.c"
|
|
|
|
```c title="binary_tree_bfs.c"
|
|
|
|
[class]{}-[func]{hierOrder}
|
|
|
|
[class]{}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_tree_bfs.cs"
|
|
|
|
```csharp title="binary_tree_bfs.cs"
|
|
|
|
[class]{binary_tree_bfs}-[func]{hierOrder}
|
|
|
|
[class]{binary_tree_bfs}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="binary_tree_bfs.swift"
|
|
|
|
```swift title="binary_tree_bfs.swift"
|
|
|
|
[class]{}-[func]{hierOrder}
|
|
|
|
[class]{}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="binary_tree_bfs.zig"
|
|
|
|
```zig title="binary_tree_bfs.zig"
|
|
|
|
[class]{}-[func]{hierOrder}
|
|
|
|
[class]{}-[func]{levelOrder}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 复杂度分析
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**时间复杂度**:所有结点被访问一次,使用 $O(n)$ 时间,其中 $n$ 为结点数量。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**空间复杂度**:当为满二叉树时达到最差情况,遍历到最底层前,队列中最多同时存在 $\frac{n + 1}{2}$ 个结点,使用 $O(n)$ 空间。
|
|
|
|
|
|
|
|
|
|
|
|
## 7.2.2. 前序、中序、后序遍历
|
|
|
|
## 7.2.2. 前序、中序、后序遍历
|
|
|
|
|
|
|
|
|
|
|
|
相对地,前、中、后序遍历皆属于「深度优先遍历 Depth-First Traversal」,其体现着一种“先走到尽头,再回头继续”的回溯遍历方式。
|
|
|
|
相对地,前、中、后序遍历皆属于「深度优先遍历 Depth-First Traversal」,其体现着一种“先走到尽头,再回头继续”的回溯遍历方式。
|
|
|
@ -100,6 +108,8 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 实现代码
|
|
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
|
|
```java title="binary_tree_dfs.java"
|
|
|
|
```java title="binary_tree_dfs.java"
|
|
|
@ -203,3 +213,9 @@ comments: true
|
|
|
|
!!! note
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
|
|
|
|
使用循环一样可以实现前、中、后序遍历,但代码相对繁琐,有兴趣的同学可以自行实现。
|
|
|
|
使用循环一样可以实现前、中、后序遍历,但代码相对繁琐,有兴趣的同学可以自行实现。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 复杂度分析
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**时间复杂度**:所有结点被访问一次,使用 $O(n)$ 时间,其中 $n$ 为结点数量。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**空间复杂度**:当树退化为链表时达到最差情况,递归深度达到 $n$ ,系统使用 $O(n)$ 栈帧空间。
|
|
|
|