Skip to content

Commit 9ef3bae

Browse files
committed
feat: add 111
1 parent b0e961c commit 9ef3bae

File tree

5 files changed

+121
-74
lines changed

5 files changed

+121
-74
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
|107|[Binary Tree Level Order Traversal II][107]|Tree, Breadth-first Search|
3535
|108|[Convert Sorted Array to Binary Search Tree][108]|Tree, Depth-first Search|
3636
|110|[Balanced Binary Tree][110]|Tree, Depth-first Search|
37+
|111|[Minimum Depth of Binary Tree][111]|Tree, Depth-first Search, Breadth-first Search|
3738

3839

3940
## Medium
@@ -83,6 +84,7 @@
8384
[107]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/107/README.md
8485
[108]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/108/README.md
8586
[110]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/110/README.md
87+
[111]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/111/README.md
8688

8789
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
8890
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

note/111/README.md

+47-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# [Balanced Binary Tree][title]
1+
# [Minimum Depth of Binary Tree][title]
22

33
## Description
44

5-
Given a binary tree, determine if it is height-balanced.
5+
Given a binary tree, find its minimum depth.
66

7-
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
7+
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
88

9-
**Tags:** Tree, Depth-first Search
9+
**Tags:** Tree, Depth-first Search, Breadth-first Search
1010

1111

12-
## 思路
12+
## 思路0
1313

14-
题意是判断一棵二叉树是否是高度平衡的,所谓二叉树高度平衡指的是二叉树的每个节点的两棵子树的高度差都不超过1,那么我们只需计算左右子树的高度,判断其高度差是否不超过1即可,如果超过1,就代表其不是高度平衡的,立即返回不是即可,我这里用返回`-1`代表不是
14+
题意是查找二叉树的最小深度,也就是找到从根结点到叶子节点的最小深度,最容易想到的当然是深搜,如果节点的左右深度都不是0的话,说明该节点含有左右子树,所以它的最小高度就是1加上其左右子树高度较小者,否则如果左子树为空或者右子树为空或者两者都为空,那么就是1加上非空子树高度
1515

1616

1717
``` java
@@ -25,18 +25,49 @@ For this problem, a height-balanced binary tree is defined as a binary tree in w
2525
* }
2626
*/
2727
class Solution {
28-
public boolean isBalanced(TreeNode root) {
29-
return helper(root) != -1;
28+
public int minDepth(TreeNode root) {
29+
if (root == null) return 0;
30+
int l = minDepth(root.left);
31+
int r = minDepth(root.right);
32+
if (l != 0 && r != 0) return 1 + Math.min(l, r);
33+
return l + r + 1;
3034
}
35+
}
36+
```
37+
38+
## 思路1
39+
40+
第二种思路就是利用宽搜了,搜索到该层有叶子节点,那就返回该层宽度即可。
3141

32-
private int helper(TreeNode node) {
33-
if (node == null) return 0;
34-
int l = helper(node.left);
35-
if (l == -1) return -1;
36-
int r = helper(node.right);
37-
if (r == -1) return -1;
38-
if (Math.abs(l - r) > 1) return -1;
39-
return 1 + Math.max(l, r);
42+
``` java
43+
/**
44+
* Definition for a binary tree node.
45+
* public class TreeNode {
46+
* int val;
47+
* TreeNode left;
48+
* TreeNode right;
49+
* TreeNode(int x) { val = x; }
50+
* }
51+
*/
52+
class Solution {
53+
public int minDepth(TreeNode root) {
54+
if (root == null) return 0;
55+
LinkedList<TreeNode> q = new LinkedList<>();
56+
q.add(root);
57+
int ans = 1;
58+
while (!q.isEmpty()) {
59+
int size = q.size();
60+
for (int i = 0; i < size; ++i) {
61+
TreeNode node = q.remove();
62+
if (node.left == null && node.right == null) {
63+
return ans;
64+
}
65+
if (node.left != null) q.add(node.left);
66+
if (node.right != null) q.add(node.right);
67+
}
68+
++ans;
69+
}
70+
return 520;
4071
}
4172
}
4273
```

project/leetcode/.idea/workspace.xml

+46-54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

project/leetcode/src/com/blankj/easy/_111/Solution.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import com.blankj.structure.TreeNode;
55

6+
import java.util.LinkedList;
7+
68
/**
79
* <pre>
810
* author: Blankj
@@ -12,12 +14,32 @@
1214
* </pre>
1315
*/
1416
public class Solution {
17+
// public int minDepth(TreeNode root) {
18+
// if (root == null) return 0;
19+
// int l = minDepth(root.left);
20+
// int r = minDepth(root.right);
21+
// if (l != 0 && r != 0) return 1 + Math.min(l, r);
22+
// return l + r + 1;
23+
// }
24+
1525
public int minDepth(TreeNode root) {
1626
if (root == null) return 0;
17-
int l = minDepth(root.left);
18-
int r = minDepth(root.right);
19-
if (l == 0 || r == 0) return l + r + 1;
20-
return 1 + Math.min(l, r);
27+
LinkedList<TreeNode> q = new LinkedList<>();
28+
q.add(root);
29+
int ans = 1;
30+
while (!q.isEmpty()) {
31+
int size = q.size();
32+
for (int i = 0; i < size; ++i) {
33+
TreeNode node = q.remove();
34+
if (node.left == null && node.right == null) {
35+
return ans;
36+
}
37+
if (node.left != null) q.add(node.left);
38+
if (node.right != null) q.add(node.right);
39+
}
40+
++ans;
41+
}
42+
return 520;
2143
}
2244

2345
public static void main(String[] args) {

0 commit comments

Comments
 (0)