Skip to content

Commit 5f228c3

Browse files
committed
feat: add solutions to lc problem: No.0111. Minimum Depth of Binary Tree
1 parent 964a38f commit 5f228c3

File tree

5 files changed

+129
-48
lines changed

5 files changed

+129
-48
lines changed

solution/0100-0199/0111.Minimum Depth of Binary Tree/README.md

+47-15
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<li><code>-1000 <= Node.val <= 1000</code></li>
3838
</ul>
3939

40-
4140
## 解法
4241

4342
<!-- 这里可写通用的实现逻辑 -->
@@ -59,15 +58,13 @@ class Solution:
5958
def minDepth(self, root: TreeNode) -> int:
6059
if root is None:
6160
return 0
62-
if root.left is None and root.right is None:
63-
return 1
64-
l = self.minDepth(root.left)
65-
r = self.minDepth(root.right)
66-
# 如果左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度
67-
if root.left is None or root.right is None:
68-
return l + r + 1
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)
6966
# 左右子树都不为空,返回最小深度+1即可
70-
return min(l, r) + 1
67+
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
7168
```
7269

7370
### **Java**
@@ -92,16 +89,51 @@ class Solution:
9289
*/
9390
class Solution {
9491
public int minDepth(TreeNode root) {
95-
if (root == null) return 0;
96-
if (root.left == null && root.right == null) return 1;
97-
int l = minDepth(root.left);
98-
int r = minDepth(root.right);
99-
if (root.left == null || root.right == null) return l + r + 1;
100-
return Math.min(l, r) + 1;
92+
if (root == null) {
93+
return 0;
94+
}
95+
if (root.left == null) {
96+
return 1 + minDepth(root.right);
97+
}
98+
if (root.right == null) {
99+
return 1 + minDepth(root.left);
100+
}
101+
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
101102
}
102103
}
103104
```
104105

106+
### **C++**
107+
108+
```cpp
109+
/**
110+
* Definition for a binary tree node.
111+
* struct TreeNode {
112+
* int val;
113+
* TreeNode *left;
114+
* TreeNode *right;
115+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
116+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
117+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
118+
* };
119+
*/
120+
class Solution {
121+
public:
122+
int minDepth(TreeNode* root) {
123+
if (root == nullptr) {
124+
return 0;
125+
}
126+
if (root->left == nullptr) {
127+
return 1 + minDepth(root->right);
128+
}
129+
if (root->right == nullptr) {
130+
return 1 + minDepth(root->left);
131+
}
132+
return 1 + min(minDepth(root->left), minDepth(root->right));
133+
}
134+
};
135+
```
136+
105137
### **...**
106138
107139
```

solution/0100-0199/0111.Minimum Depth of Binary Tree/README_EN.md

+46-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
3434
</ul>
3535

36-
3736
## Solutions
3837

3938
<!-- tabs:start -->
@@ -51,15 +50,11 @@ class Solution:
5150
def minDepth(self, root: TreeNode) -> int:
5251
if root is None:
5352
return 0
54-
if root.left is None and root.right is None:
55-
return 1
56-
l = self.minDepth(root.left)
57-
r = self.minDepth(root.right)
58-
# 如果左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度
59-
if root.left is None or root.right is None:
60-
return l + r + 1
61-
# 左右子树都不为空,返回最小深度+1即可
62-
return min(l, r) + 1
53+
if root.left is None:
54+
return 1 + self.minDepth(root.right)
55+
if root.right is None:
56+
return 1 + self.minDepth(root.left)
57+
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
6358
```
6459

6560
### **Java**
@@ -82,16 +77,51 @@ class Solution:
8277
*/
8378
class Solution {
8479
public int minDepth(TreeNode root) {
85-
if (root == null) return 0;
86-
if (root.left == null && root.right == null) return 1;
87-
int l = minDepth(root.left);
88-
int r = minDepth(root.right);
89-
if (root.left == null || root.right == null) return l + r + 1;
90-
return Math.min(l, r) + 1;
80+
if (root == null) {
81+
return 0;
82+
}
83+
if (root.left == null) {
84+
return 1 + minDepth(root.right);
85+
}
86+
if (root.right == null) {
87+
return 1 + minDepth(root.left);
88+
}
89+
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
9190
}
9291
}
9392
```
9493

94+
### **C++**
95+
96+
```cpp
97+
/**
98+
* Definition for a binary tree node.
99+
* struct TreeNode {
100+
* int val;
101+
* TreeNode *left;
102+
* TreeNode *right;
103+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
104+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
105+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
106+
* };
107+
*/
108+
class Solution {
109+
public:
110+
int minDepth(TreeNode* root) {
111+
if (root == nullptr) {
112+
return 0;
113+
}
114+
if (root->left == nullptr) {
115+
return 1 + minDepth(root->right);
116+
}
117+
if (root->right == nullptr) {
118+
return 1 + minDepth(root->left);
119+
}
120+
return 1 + min(minDepth(root->left), minDepth(root->right));
121+
}
122+
};
123+
```
124+
95125
### **...**
96126
97127
```
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
112
class Solution {
213
public:
314
int minDepth(TreeNode* root) {
4-
if (!root) return 0;
5-
if (!root->left) return 1 + minDepth(root->right);
6-
if (!root->right) return 1 + minDepth(root->left);
15+
if (root == nullptr) {
16+
return 0;
17+
}
18+
if (root->left == nullptr) {
19+
return 1 + minDepth(root->right);
20+
}
21+
if (root->right == nullptr) {
22+
return 1 + minDepth(root->left);
23+
}
724
return 1 + min(minDepth(root->left), minDepth(root->right));
825
}
926
};

solution/0100-0199/0111.Minimum Depth of Binary Tree/Solution.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
*/
1616
class Solution {
1717
public int minDepth(TreeNode root) {
18-
if (root == null) return 0;
19-
if (root.left == null && root.right == null) return 1;
20-
int l = minDepth(root.left);
21-
int r = minDepth(root.right);
22-
if (root.left == null || root.right == null) return l + r + 1;
23-
return Math.min(l, r) + 1;
18+
if (root == null) {
19+
return 0;
20+
}
21+
if (root.left == null) {
22+
return 1 + minDepth(root.right);
23+
}
24+
if (root.right == null) {
25+
return 1 + minDepth(root.left);
26+
}
27+
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
2428
}
2529
}

solution/0100-0199/0111.Minimum Depth of Binary Tree/Solution.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ class Solution:
88
def minDepth(self, root: TreeNode) -> int:
99
if root is None:
1010
return 0
11-
if root.left is None and root.right is None:
12-
return 1
13-
l = self.minDepth(root.left)
14-
r = self.minDepth(root.right)
15-
# 如果左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度
16-
if root.left is None or root.right is None:
17-
return l + r + 1
11+
# 如果左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度+1
12+
if root.left is None:
13+
return 1 + self.minDepth(root.right)
14+
if root.right is None:
15+
return 1 + self.minDepth(root.left)
1816
# 左右子树都不为空,返回最小深度+1即可
19-
return min(l, r) + 1
17+
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))

0 commit comments

Comments
 (0)