Skip to content

Commit 10480c6

Browse files
committed
[0203]UPDATE:LC-111BFS
1 parent ae1b4eb commit 10480c6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: 树/111. 二叉树的最小深度-Easy.md

+26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@
2525

2626

2727
# Solution
28+
### 1. BFS 效率更高,但有额外空间复杂度
29+
```python
30+
class Solution:
31+
def minDepth(self, root: TreeNode) -> int:
32+
if not root:
33+
return 0
34+
queue = [root]
35+
depth = 1
36+
37+
while queue:
38+
tmp_length = len(queue)
39+
40+
for i in range(tmp_length):
41+
tmp_node = queue.pop(0)
42+
if not tmp_node.left and not tmp_node.right:
43+
return depth
44+
45+
if tmp_node.left:
46+
queue.append(tmp_node.left)
47+
if tmp_node.right:
48+
queue.append(tmp_node.right)
49+
50+
depth += 1
51+
```
52+
53+
### 2. DFS
2854
- 时间复杂度:$O(logn)$
2955
- 空间复杂度:$O(1)$
3056

0 commit comments

Comments
 (0)