Skip to content

Commit 8631e8b

Browse files
committed
再次完成 111
1 parent 3fdad74 commit 8631e8b

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

docs/0111-minimum-depth-of-binary-tree.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[#0111-minimum-depth-of-binary-tree]
12
= 111. Minimum Depth of Binary Tree
23

34
https://leetcode.com/problems/minimum-depth-of-binary-tree/[LeetCode - Minimum Depth of Binary Tree]
@@ -42,3 +43,18 @@ return its minimum depth = 2.
4243
include::{sourcedir}/_0111_MinimumDepthOfBinaryTree.java[]
4344
----
4445

46+
47+
[{java_src_attr}]
48+
----
49+
include::{sourcedir}/_0111_MinimumDepthOfBinaryTree_2.java[]
50+
----
51+
52+
53+
TIP: 可以换个角度理解这道题:使用 Morris 遍历树时,遍历到每个节点时,计算该节点的深度,然后从中筛选出叶子阶段的深度,取最小值即可。
54+
55+
[{java_src_attr}]
56+
----
57+
include::{sourcedir}/_0111_MinimumDepthOfBinaryTree_Morris.java[]
58+
----
59+
60+
竟然在 LeetCode 中文站点没有找到使用 Morris 遍历方法处理的解答。
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import com.diguage.algorithm.util.TreeNode;
4+
5+
import java.util.Objects;
6+
7+
import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
8+
import static java.util.Arrays.asList;
9+
10+
/**
11+
* = 111. Minimum Depth of Binary Tree
12+
*
13+
* https://leetcode.com/problems/minimum-depth-of-binary-tree/[Minimum Depth of Binary Tree - LeetCode]
14+
*
15+
* @author D瓜哥, https://www.diguage.com/
16+
* @since 2020-02-07 21:26
17+
*/
18+
public class _0111_MinimumDepthOfBinaryTree_2 {
19+
20+
public int minDepth(TreeNode root) {
21+
if (Objects.isNull(root)) {
22+
return 0;
23+
}
24+
if (root.left == null && root.right == null) {
25+
return 1;
26+
}
27+
int result = Integer.MAX_VALUE;
28+
if (Objects.nonNull(root.left)) {
29+
result = Math.min(result, minDepth(root.left));
30+
}
31+
if (Objects.nonNull(root.right)) {
32+
result = Math.min(result, minDepth(root.right));
33+
}
34+
return result + 1;
35+
}
36+
37+
public static void main(String[] args) {
38+
_0111_MinimumDepthOfBinaryTree_2 solution = new _0111_MinimumDepthOfBinaryTree_2();
39+
int r1 = solution.minDepth(buildTree(asList(3, 9, 20, null, null, 15, 7)));
40+
System.out.println((r1 == 2) + " : " + r1);
41+
}
42+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import com.diguage.algorithm.util.TreeNode;
4+
5+
import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
6+
import static java.util.Arrays.asList;
7+
8+
/**
9+
* = 111. Minimum Depth of Binary Tree
10+
*
11+
* https://leetcode.com/problems/minimum-depth-of-binary-tree/[Minimum Depth of Binary Tree - LeetCode]
12+
*
13+
* @author D瓜哥, https://www.diguage.com/
14+
* @since 2020-02-07 21:26
15+
*/
16+
public class _0111_MinimumDepthOfBinaryTree_Morris {
17+
18+
/**
19+
* 参考左程云《程序员代码面试指南》的解法
20+
*/
21+
public int minDepth(TreeNode head) {
22+
if (head == null) {
23+
return 0;
24+
}
25+
TreeNode cur = head;
26+
TreeNode mostRight = null;
27+
int curLevel = 0;
28+
int minHeight = Integer.MAX_VALUE;
29+
while (cur != null) {
30+
mostRight = cur.left;
31+
if (mostRight != null) { // 当前 cur 有左子树,能到达两次
32+
// cur 左子树上,右边界的节点个数
33+
int leftTreeRightSize = 1;
34+
// 找到 cur 左子树上最右边的节点
35+
while (mostRight.right != null && mostRight.right != cur) {
36+
leftTreeRightSize++;
37+
mostRight = mostRight.right;
38+
}
39+
if (mostRight.right == null) {
40+
// 第一次到达 cur,那么下一个节点的 level 必然+1
41+
curLevel++;
42+
mostRight.right = cur;
43+
cur = cur.left;
44+
continue;
45+
} else {
46+
// 第二次到达cur,那么下一个节点的 level = curLevel - leftTreeRightSize
47+
// 此时检查 mostRight 是不是叶节点,记录答案
48+
if (mostRight.left == null) {
49+
minHeight = Math.min(minHeight, curLevel);
50+
}
51+
curLevel -= leftTreeRightSize;
52+
mostRight.right = null;
53+
}
54+
} else {
55+
// 当前 cur 没有左子树,只能到达有ICI,那么下一个节点的 level 必然+1
56+
curLevel++;
57+
}
58+
cur = cur.right;
59+
}
60+
int finalRight = 1;
61+
cur = head;
62+
while (cur.right != null) {
63+
finalRight++;
64+
cur = cur.right;
65+
}
66+
// 最后不要忘了单独看一看整棵树的最右节点是不是叶节点
67+
if (cur.left == null && cur.right == null) {
68+
minHeight = Math.min(minHeight, finalRight);
69+
}
70+
return minHeight;
71+
}
72+
73+
public static void main(String[] args) {
74+
_0111_MinimumDepthOfBinaryTree_Morris solution = new _0111_MinimumDepthOfBinaryTree_Morris();
75+
int r1 = solution.minDepth(buildTree(asList(3, 9, 20, null, null, 15, 7)));
76+
System.out.println((r1 == 2) + " : " + r1);
77+
}
78+
}

0 commit comments

Comments
 (0)