41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
- 若左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度加 1;左右子树都不为空,返回最小深度加 1 即可。
44
+ ** 方法一:递归**
45
+
46
+ 递归的终止条件是当前节点为空,此时返回 $0$;如果当前节点左右子树有一个为空,返回不为空的子树的最小深度加 $1$;如果当前节点左右子树都不为空,返回左右子树最小深度的较小值加 $1$。
47
+
48
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。
49
+
50
+ ** 方法二:BFS**
51
+
52
+ 使用队列实现广度优先搜索,初始时将根节点加入队列。每次从队列中取出一个节点,如果该节点是叶子节点,则直接返回当前深度;如果该节点不是叶子节点,则将该节点的所有非空子节点加入队列。继续搜索下一层节点,直到找到叶子节点。
53
+
54
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。
45
55
46
56
<!-- tabs:start -->
47
57
57
67
# self.left = left
58
68
# self.right = right
59
69
class Solution :
60
- def minDepth (self , root : TreeNode) -> int :
61
- def dfs (root ):
62
- if root is None :
63
- return 0
64
- if root.left is None :
65
- return 1 + dfs(root.right)
66
- if root.right is None :
67
- return 1 + dfs(root.left)
68
- return 1 + min (dfs(root.left), dfs(root.right))
69
-
70
- return dfs(root)
70
+ def minDepth (self , root : Optional[TreeNode]) -> int :
71
+ if root is None :
72
+ return 0
73
+ if root.left is None :
74
+ return 1 + self .minDepth(root.right)
75
+ if root.right is None :
76
+ return 1 + self .minDepth(root.left)
77
+ return 1 + min (self .minDepth(root.left), self .minDepth(root.right))
78
+ ```
79
+
80
+ ``` python
81
+ # Definition for a binary tree node.
82
+ # class TreeNode:
83
+ # def __init__(self, val=0, left=None, right=None):
84
+ # self.val = val
85
+ # self.left = left
86
+ # self.right = right
87
+ class Solution :
88
+ def minDepth (self , root : Optional[TreeNode]) -> int :
89
+ if root is None :
90
+ return 0
91
+ q = deque([root])
92
+ ans = 0
93
+ while 1 :
94
+ ans += 1
95
+ for _ in range (len (q)):
96
+ node = q.popleft()
97
+ if node.left is None and node.right is None :
98
+ return ans
99
+ if node.left:
100
+ q.append(node.left)
101
+ if node.right:
102
+ q.append(node.right)
71
103
```
72
104
73
105
### ** Java**
@@ -92,20 +124,59 @@ class Solution:
92
124
*/
93
125
class Solution {
94
126
public int minDepth (TreeNode root ) {
95
- return dfs(root);
96
- }
97
-
98
- private int dfs (TreeNode root ) {
99
127
if (root == null ) {
100
128
return 0 ;
101
129
}
102
130
if (root. left == null ) {
103
- return 1 + dfs (root. right);
131
+ return 1 + minDepth (root. right);
104
132
}
105
133
if (root. right == null ) {
106
- return 1 + dfs(root. left);
134
+ return 1 + minDepth(root. left);
135
+ }
136
+ return 1 + Math . min(minDepth(root. left), minDepth(root. right));
137
+ }
138
+ }
139
+ ```
140
+
141
+ ``` java
142
+ /**
143
+ * Definition for a binary tree node.
144
+ * public class TreeNode {
145
+ * int val;
146
+ * TreeNode left;
147
+ * TreeNode right;
148
+ * TreeNode() {}
149
+ * TreeNode(int val) { this.val = val; }
150
+ * TreeNode(int val, TreeNode left, TreeNode right) {
151
+ * this.val = val;
152
+ * this.left = left;
153
+ * this.right = right;
154
+ * }
155
+ * }
156
+ */
157
+ class Solution {
158
+ public int minDepth (TreeNode root ) {
159
+ if (root == null ) {
160
+ return 0 ;
161
+ }
162
+ Deque<TreeNode > q = new ArrayDeque<> ();
163
+ q. offer(root);
164
+ int ans = 0 ;
165
+ while (true ) {
166
+ ++ ans;
167
+ for (int n = q. size(); n > 0 ; n-- ) {
168
+ TreeNode node = q. poll();
169
+ if (node. left == null && node. right == null ) {
170
+ return ans;
171
+ }
172
+ if (node. left != null ) {
173
+ q. offer(node. left);
174
+ }
175
+ if (node. right != null ) {
176
+ q. offer(node. right);
177
+ }
178
+ }
107
179
}
108
- return 1 + Math . min(dfs(root. left), dfs(root. right));
109
180
}
110
181
}
111
182
```
@@ -127,14 +198,56 @@ class Solution {
127
198
class Solution {
128
199
public:
129
200
int minDepth(TreeNode* root) {
130
- return dfs(root);
201
+ if (!root) {
202
+ return 0;
203
+ }
204
+ if (!root->left) {
205
+ return 1 + minDepth(root->right);
206
+ }
207
+ if (!root->right) {
208
+ return 1 + minDepth(root->left);
209
+ }
210
+ return 1 + min(minDepth(root->left), minDepth(root->right));
131
211
}
212
+ };
213
+ ```
132
214
133
- int dfs(TreeNode* root) {
134
- if (!root) return 0;
135
- if (!root->left) return 1 + dfs(root->right);
136
- if (!root->right) return 1 + dfs(root->left);
137
- return 1 + min(dfs(root->left), dfs(root->right));
215
+ ```cpp
216
+ /**
217
+ * Definition for a binary tree node.
218
+ * struct TreeNode {
219
+ * int val;
220
+ * TreeNode *left;
221
+ * TreeNode *right;
222
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
223
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
224
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
225
+ * };
226
+ */
227
+ class Solution {
228
+ public:
229
+ int minDepth(TreeNode* root) {
230
+ if (!root) {
231
+ return 0;
232
+ }
233
+ queue<TreeNode*> q{{root}};
234
+ int ans = 0;
235
+ while (1) {
236
+ ++ans;
237
+ for (int n = q.size(); n; --n) {
238
+ auto node = q.front();
239
+ q.pop();
240
+ if (!node->left && !node->right) {
241
+ return ans;
242
+ }
243
+ if (node->left) {
244
+ q.push(node->left);
245
+ }
246
+ if (node->right) {
247
+ q.push(node->right);
248
+ }
249
+ }
250
+ }
138
251
}
139
252
};
140
253
```
@@ -151,20 +264,16 @@ public:
151
264
* }
152
265
*/
153
266
func minDepth (root *TreeNode ) int {
154
- var dfs func (root *TreeNode) int
155
- dfs = func (root *TreeNode) int {
156
- if root == nil {
157
- return 0
158
- }
159
- if root.Left == nil {
160
- return 1 + dfs (root.Right )
161
- }
162
- if root.Right == nil {
163
- return 1 + dfs (root.Left )
164
- }
165
- return 1 + min (dfs (root.Left ), dfs (root.Right ))
267
+ if root == nil {
268
+ return 0
269
+ }
270
+ if root.Left == nil {
271
+ return 1 + minDepth (root.Right )
272
+ }
273
+ if root.Right == nil {
274
+ return 1 + minDepth (root.Left )
166
275
}
167
- return dfs ( root)
276
+ return 1 + min ( minDepth ( root. Left ), minDepth (root. Right ) )
168
277
}
169
278
170
279
func min (a , b int ) int {
@@ -175,6 +284,39 @@ func min(a, b int) int {
175
284
}
176
285
```
177
286
287
+ ``` go
288
+ /* *
289
+ * Definition for a binary tree node.
290
+ * type TreeNode struct {
291
+ * Val int
292
+ * Left *TreeNode
293
+ * Right *TreeNode
294
+ * }
295
+ */
296
+ func minDepth (root *TreeNode ) (ans int ) {
297
+ if root == nil {
298
+ return 0
299
+ }
300
+ q := []*TreeNode{root}
301
+ for {
302
+ ans++
303
+ for n := len (q); n > 0 ; n-- {
304
+ node := q[0 ]
305
+ q = q[1 :]
306
+ if node.Left == nil && node.Right == nil {
307
+ return
308
+ }
309
+ if node.Left != nil {
310
+ q = append (q, node.Left )
311
+ }
312
+ if node.Right != nil {
313
+ q = append (q, node.Right )
314
+ }
315
+ }
316
+ }
317
+ }
318
+ ```
319
+
178
320
### ** JavaScript**
179
321
180
322
``` js
@@ -191,13 +333,53 @@ func min(a, b int) int {
191
333
* @return {number}
192
334
*/
193
335
var minDepth = function (root ) {
194
- function dfs (root ) {
195
- if (! root) return 0 ;
196
- if (! root .left ) return 1 + dfs (root .right );
197
- if (! root .right ) return 1 + dfs (root .left );
198
- return 1 + Math .min (dfs (root .left ), dfs (root .right ));
336
+ if (! root) {
337
+ return 0 ;
338
+ }
339
+ if (! root .left ) {
340
+ return 1 + minDepth (root .right );
341
+ }
342
+ if (! root .right ) {
343
+ return 1 + minDepth (root .left );
344
+ }
345
+ return 1 + Math .min (minDepth (root .left ), minDepth (root .right ));
346
+ };
347
+ ```
348
+
349
+ ``` js
350
+ /**
351
+ * Definition for a binary tree node.
352
+ * function TreeNode(val, left, right) {
353
+ * this.val = (val===undefined ? 0 : val)
354
+ * this.left = (left===undefined ? null : left)
355
+ * this.right = (right===undefined ? null : right)
356
+ * }
357
+ */
358
+ /**
359
+ * @param {TreeNode} root
360
+ * @return {number}
361
+ */
362
+ var minDepth = function (root ) {
363
+ if (! root) {
364
+ return 0 ;
365
+ }
366
+ const q = [root];
367
+ let ans = 0 ;
368
+ while (1 ) {
369
+ ++ ans;
370
+ for (let n = q .length ; n; -- n) {
371
+ const node = q .shift ();
372
+ if (! node .left && ! node .right ) {
373
+ return ans;
374
+ }
375
+ if (node .left ) {
376
+ q .push (node .left );
377
+ }
378
+ if (node .right ) {
379
+ q .push (node .right );
380
+ }
381
+ }
199
382
}
200
- return dfs (root);
201
383
};
202
384
```
203
385
@@ -233,6 +415,45 @@ function minDepth(root: TreeNode | null): number {
233
415
}
234
416
```
235
417
418
+ ``` ts
419
+ /**
420
+ * Definition for a binary tree node.
421
+ * class TreeNode {
422
+ * val: number
423
+ * left: TreeNode | null
424
+ * right: TreeNode | null
425
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
426
+ * this.val = (val===undefined ? 0 : val)
427
+ * this.left = (left===undefined ? null : left)
428
+ * this.right = (right===undefined ? null : right)
429
+ * }
430
+ * }
431
+ */
432
+
433
+ function minDepth(root : TreeNode | null ): number {
434
+ if (! root ) {
435
+ return 0 ;
436
+ }
437
+ const q = [root ];
438
+ let ans = 0 ;
439
+ while (1 ) {
440
+ ++ ans ;
441
+ for (let n = q .length ; n ; -- n ) {
442
+ const node = q .shift ();
443
+ if (! node .left && ! node .right ) {
444
+ return ans ;
445
+ }
446
+ if (node .left ) {
447
+ q .push (node .left );
448
+ }
449
+ if (node .right ) {
450
+ q .push (node .right );
451
+ }
452
+ }
453
+ }
454
+ }
455
+ ```
456
+
236
457
### ** Rust**
237
458
238
459
``` rust
0 commit comments