Skip to content

Commit 7606f0d

Browse files
committed
feat: add solutions to leetcode problem: No.0111
See https://leetcode-cn.com/problems/minimum-depth-of-binary-tree
1 parent 8f34b0a commit 7606f0d

File tree

6 files changed

+121
-13
lines changed

6 files changed

+121
-13
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
1. [二叉树的层次遍历](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README.md)
8888
1. [二叉树的层次遍历 II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README.md)
8989
1. [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)
90+
1. [二叉树的最小深度](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README.md)
9091
1. [从前序与中序遍历序列构造二叉树](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README.md)
9192
1. [从中序与后序遍历序列构造二叉树](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README.md)
9293
1. [二叉树的最近公共祖先](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
8585
1. [Binary Tree Level Order Traversal](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README_EN.md)
8686
1. [Binary Tree Level Order Traversal II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README_EN.md)
8787
1. [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)
88+
1. [Minimum Depth of Binary Tree](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README_EN.md)
8889
1. [Construct Binary Tree from Preorder and Inorder Traversal](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README_EN.md)
8990
1. [Construct Binary Tree from Inorder and Postorder Traversal](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README_EN.md)
9091
1. [Lowest Common Ancestor of a Binary Tree](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)

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

+44-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,57 @@
3434
<!-- 这里可写当前语言的特殊实现逻辑 -->
3535

3636
```python
37-
37+
# Definition for a binary tree node.
38+
# class TreeNode:
39+
# def __init__(self, val=0, left=None, right=None):
40+
# self.val = val
41+
# self.left = left
42+
# self.right = right
43+
class Solution:
44+
def minDepth(self, root: TreeNode) -> int:
45+
if root is None:
46+
return 0
47+
if root.left is None and root.right is None:
48+
return 1
49+
l = self.minDepth(root.left)
50+
r = self.minDepth(root.right)
51+
# 如果左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度
52+
if root.left is None or root.right is None:
53+
return l + r + 1
54+
# 左右子树都不为空,返回最小深度+1即可
55+
return min(l, r) + 1
3856
```
3957

4058
### **Java**
4159

4260
<!-- 这里可写当前语言的特殊实现逻辑 -->
4361

4462
```java
45-
63+
/**
64+
* Definition for a binary tree node.
65+
* public class TreeNode {
66+
* int val;
67+
* TreeNode left;
68+
* TreeNode right;
69+
* TreeNode() {}
70+
* TreeNode(int val) { this.val = val; }
71+
* TreeNode(int val, TreeNode left, TreeNode right) {
72+
* this.val = val;
73+
* this.left = left;
74+
* this.right = right;
75+
* }
76+
* }
77+
*/
78+
class Solution {
79+
public int minDepth(TreeNode root) {
80+
if (root == null) return 0;
81+
if (root.left == null && root.right == null) return 1;
82+
int l = minDepth(root.left);
83+
int r = minDepth(root.right);
84+
if (root.left == null || root.right == null) return l + r + 1;
85+
return Math.min(l, r) + 1;
86+
}
87+
}
4688
```
4789

4890
### **...**

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

+44-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,55 @@
3535
### **Python3**
3636

3737
```python
38-
38+
# Definition for a binary tree node.
39+
# class TreeNode:
40+
# def __init__(self, val=0, left=None, right=None):
41+
# self.val = val
42+
# self.left = left
43+
# self.right = right
44+
class Solution:
45+
def minDepth(self, root: TreeNode) -> int:
46+
if root is None:
47+
return 0
48+
if root.left is None and root.right is None:
49+
return 1
50+
l = self.minDepth(root.left)
51+
r = self.minDepth(root.right)
52+
# 如果左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度
53+
if root.left is None or root.right is None:
54+
return l + r + 1
55+
# 左右子树都不为空,返回最小深度+1即可
56+
return min(l, r) + 1
3957
```
4058

4159
### **Java**
4260

4361
```java
44-
62+
/**
63+
* Definition for a binary tree node.
64+
* public class TreeNode {
65+
* int val;
66+
* TreeNode left;
67+
* TreeNode right;
68+
* TreeNode() {}
69+
* TreeNode(int val) { this.val = val; }
70+
* TreeNode(int val, TreeNode left, TreeNode right) {
71+
* this.val = val;
72+
* this.left = left;
73+
* this.right = right;
74+
* }
75+
* }
76+
*/
77+
class Solution {
78+
public int minDepth(TreeNode root) {
79+
if (root == null) return 0;
80+
if (root.left == null && root.right == null) return 1;
81+
int l = minDepth(root.left);
82+
int r = minDepth(root.right);
83+
if (root.left == null || root.right == null) return l + r + 1;
84+
return Math.min(l, r) + 1;
85+
}
86+
}
4587
```
4688

4789
### **...**

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
* int val;
55
* TreeNode left;
66
* TreeNode right;
7-
* TreeNode(int x) { val = x; }
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
814
* }
915
*/
1016
class Solution {
1117
public int minDepth(TreeNode root) {
1218
if (root == null) return 0;
13-
14-
if (root.left == null && root.right != null) {
15-
return 1 + minDepth(root.right);
16-
}
17-
if (root.right == null && root.left != null) {
18-
return 1 + minDepth(root.left);
19-
}
20-
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
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;
2124
}
2225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def minDepth(self, root: TreeNode) -> int:
9+
if root is None:
10+
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
18+
# 左右子树都不为空,返回最小深度+1即可
19+
return min(l, r) + 1

0 commit comments

Comments
 (0)