We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ae1b4eb commit 10480c6Copy full SHA for 10480c6
树/111. 二叉树的最小深度-Easy.md
@@ -25,6 +25,32 @@
25
26
27
# 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
54
- 时间复杂度:$O(logn)$
55
- 空间复杂度:$O(1)$
56
0 commit comments