Skip to content

Commit 29ccfcc

Browse files
committed
📚sort
1 parent 0ae1c1e commit 29ccfcc

File tree

6 files changed

+459
-179
lines changed

6 files changed

+459
-179
lines changed

docs/.DS_Store

0 Bytes
Binary file not shown.

docs/.obsidian/workspace.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@
213213
},
214214
"active": "e24a131a4a8c13f9",
215215
"lastOpenFiles": [
216+
"data-structure-algorithms/algorithm/BFS.md",
217+
"data-structure-algorithms/algorithm/Untitled.md",
216218
"interview/Algorithm.md",
217219
"interview/Alto.md",
218220
"interview/Untitled.md",
@@ -238,8 +240,6 @@
238240
"2024-08-05.md",
239241
"java/JVM/GC.md",
240242
"interview/RPC-FAQ~.md",
241-
"interview/Ability-FAQ~.md",
242-
"interview/Java-Develop-FAQ~.md",
243243
"data-structure-algorithms/algorithm",
244244
"data-structure-algorithms/未命名文件夹"
245245
]
0 Bytes
Binary file not shown.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
3+
4+
5+
6+
7+
### [二叉树的最小深度_111](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)
8+
9+
> 给定一个二叉树,找出其最小深度。
10+
>
11+
> 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
12+
>
13+
> 说明:叶子节点是指没有子节点的节点。
14+
>
15+
> 示例 1:
16+
>
17+
> ![img](https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg)
18+
>
19+
> ```
20+
> 输入:root = [3,9,20,null,null,15,7]
21+
> 输出:2
22+
> ```
23+
>
24+
25+
思路:这里注意这个 `while` 循环和 `for` 循环的配合,**`while` 循环控制一层一层往下走,`for` 循环利用 `sz` 变量控制从左到右遍历每一层二叉树节点**:
26+
27+
![img](https://labuladong.online/algo/images/dijkstra/1.jpeg)
28+
29+
```java
30+
class Solution {
31+
public int minDepth(TreeNode root) {
32+
if (root == null) return 0;
33+
Queue<TreeNode> q = new LinkedList<>();
34+
q.offer(root);
35+
// root 本身就是一层,depth 初始化为 1
36+
int depth = 1;
37+
38+
while (!q.isEmpty()) {
39+
int sz = q.size();
40+
// 将当前队列中的所有节点向四周扩散
41+
for (int i = 0; i < sz; i++) {
42+
TreeNode cur = q.poll();
43+
// 判断是否到达终点
44+
if (cur.left == null && cur.right == null)
45+
return depth;
46+
// 将 cur 的相邻节点加入队列
47+
if (cur.left != null)
48+
q.offer(cur.left);
49+
if (cur.right != null)
50+
q.offer(cur.right);
51+
}
52+
// 这里增加步数
53+
depth++;
54+
}
55+
return depth;
56+
}
57+
}
58+
```
59+

0 commit comments

Comments
 (0)