-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumDepth.java
More file actions
71 lines (65 loc) · 2.08 KB
/
Copy pathMinimumDepth.java
File metadata and controls
71 lines (65 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package tree;
import org.junit.Test;
import utils.TreeNodeUtil;
import java.util.LinkedList;
import java.util.Queue;
/**
* @Author: wei1
* @Date: Create in 2018/12/1 2:11
* @Description: Given a binary tree, find its minimum depth.
* The minimum depth is the number of nodes along the shortest
* path from the root node down to the nearest leaf node.
* <p>
* 思路:
* 递归,若为空树返回0;
* 若左子树为空,则返回右子树的最小深度+1;(加1是因为要加上根这一层,下同)
* 若右子树为空,则返回左子树的最小深度+1;
* 若左右子树均不为空,则取左、右子树最小深度的较小值,+1;
* <p>
* 非递归,思路是层序遍历,找到第一个左右结点都为null的情况,就返回
*/
public class MinimumDepth {
public int run(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int result = 0;
while (!queue.isEmpty()) {
int len = queue.size();
result++;
for (int i = 0; i < len; i++) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
if (node.left == null && node.right == null) {
return result;
}
}
}
return result;
}
public int minimumDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null) {
return minimumDepth(root.right) + 1;
}
if (root.right == null) {
return minimumDepth(root.left) + 1;
}
return Math.min(minimumDepth(root.right), minimumDepth(root.left)) + 1;
}
@Test
public void test() {
TreeNode tree = TreeNodeUtil.getTree();
System.out.println(run(tree));
System.out.println(minimumDepth(tree));
}
}