41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
+ 若左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度加 1;左右子树都不为空,返回最小深度加 1 即可。
45
+
44
46
<!-- tabs:start -->
45
47
46
48
### ** Python3**
56
58
# self.right = right
57
59
class Solution :
58
60
def minDepth (self , root : TreeNode) -> int :
59
- if root is None :
60
- return 0
61
- # 如果左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度+1
62
- if root.left is None :
63
- return 1 + self .minDepth(root.right)
64
- if root.right is None :
65
- return 1 + self .minDepth(root.left)
66
- # 左右子树都不为空,返回最小深度+1即可
67
- return 1 + min (self .minDepth(root.left), self .minDepth(root.right))
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)
68
71
```
69
72
70
73
### ** Java**
@@ -89,16 +92,20 @@ class Solution:
89
92
*/
90
93
class Solution {
91
94
public int minDepth (TreeNode root ) {
95
+ return dfs(root);
96
+ }
97
+
98
+ private int dfs (TreeNode root ) {
92
99
if (root == null ) {
93
100
return 0 ;
94
101
}
95
102
if (root. left == null ) {
96
- return 1 + minDepth (root. right);
103
+ return 1 + dfs (root. right);
97
104
}
98
105
if (root. right == null ) {
99
- return 1 + minDepth (root. left);
106
+ return 1 + dfs (root. left);
100
107
}
101
- return 1 + Math . min(minDepth (root. left), minDepth (root. right));
108
+ return 1 + Math . min(dfs (root. left), dfs (root. right));
102
109
}
103
110
}
104
111
```
@@ -119,10 +126,13 @@ class Solution {
119
126
* @return {number}
120
127
*/
121
128
var minDepth = function (root ) {
122
- if (root == null ) return 0 ;
123
- if (root .left == null ) return minDepth (root .right ) + 1 ;
124
- if (root .right == null ) return minDepth (root .left ) + 1 ;
125
- return Math .min (minDepth (root .left ), minDepth (root .right )) + 1 ;
129
+ function dfs (root ) {
130
+ if (! root) return 0 ;
131
+ if (! root .left ) return 1 + dfs (root .right );
132
+ if (! root .right ) return 1 + dfs (root .left );
133
+ return 1 + Math .min (dfs (root .left ), dfs (root .right ));
134
+ }
135
+ return dfs (root);
126
136
};
127
137
```
128
138
@@ -143,20 +153,54 @@ var minDepth = function (root) {
143
153
class Solution {
144
154
public:
145
155
int minDepth(TreeNode* root) {
146
- if (root == nullptr) {
147
- return 0;
148
- }
149
- if (root->left == nullptr) {
150
- return 1 + minDepth(root->right);
151
- }
152
- if (root->right == nullptr) {
153
- return 1 + minDepth(root->left);
154
- }
155
- return 1 + min(minDepth(root->left), minDepth(root->right));
156
+ return dfs(root);
157
+ }
158
+
159
+ int dfs(TreeNode* root) {
160
+ if (!root) return 0;
161
+ if (!root->left) return 1 + dfs(root->right);
162
+ if (!root->right) return 1 + dfs(root->left);
163
+ return 1 + min(dfs(root->left), dfs(root->right));
156
164
}
157
165
};
158
166
```
159
167
168
+ ### ** Go**
169
+
170
+ ``` go
171
+ /* *
172
+ * Definition for a binary tree node.
173
+ * type TreeNode struct {
174
+ * Val int
175
+ * Left *TreeNode
176
+ * Right *TreeNode
177
+ * }
178
+ */
179
+ func minDepth (root *TreeNode ) int {
180
+ var dfs func (root *TreeNode) int
181
+ dfs = func (root *TreeNode) int {
182
+ if root == nil {
183
+ return 0
184
+ }
185
+ if root.Left == nil {
186
+ return 1 + dfs (root.Right )
187
+ }
188
+ if root.Right == nil {
189
+ return 1 + dfs (root.Left )
190
+ }
191
+ return 1 + min (dfs (root.Left ), dfs (root.Right ))
192
+ }
193
+ return dfs (root)
194
+ }
195
+
196
+ func min (a , b int ) int {
197
+ if a < b {
198
+ return a
199
+ }
200
+ return b
201
+ }
202
+ ```
203
+
160
204
### ** ...**
161
205
162
206
```
0 commit comments