-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinDepth.java
More file actions
81 lines (74 loc) · 2.39 KB
/
MinDepth.java
File metadata and controls
81 lines (74 loc) · 2.39 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
72
73
74
75
76
77
78
79
80
81
package problems;
import java.util.LinkedList;
/**
* 求二叉树的最小深度
*
* https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
*/
public class MinDepth {
/**
* BFS算法
*/
public int solution(TreeNode root) {
if (root == null) return 0;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
// root本身是一层
int depth = 1;
while (!queue.isEmpty()) {
int size = queue.size();
// 将当前队列中的结点向四周扩散
for (int i = 0; i < size; i++) {
TreeNode curr = queue.removeFirst();
// 判断是否到达结束条件
if (curr.left == null && curr.right == null) return depth;
// 将结点的下一层孩子结点加入到队列
if (curr.left != null) {
queue.add(curr.left);
}
if (curr.right != null) {
queue.add(curr.right);
}
}
// 增加多一层
depth++;
}
return depth;
}
// 树结点
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
// 给定值序列构造一棵树
private TreeNode buildTree(Integer[] values) {
TreeNode[] nodes = new TreeNode[values.length];
for (int i = 0; i < values.length; i++) {
if (i == 0 && values[i] != null) {
nodes[0] = new TreeNode(values[i]);
} else if (values[i] != null) {
nodes[i] = new TreeNode(values[i]);
int parentIndex = (i - 1) / 2;
if ((i - 1) % 2 == 0) { // left
nodes[parentIndex].left = nodes[i];
} else { // right
nodes[parentIndex].right = nodes[i];
}
}
}
return nodes[0];
}
public static void main(String[] args) {
MinDepth minDepth = new MinDepth();
TreeNode node = minDepth.buildTree(new Integer[]{3, 9, 20, null, null, 15, 7});
System.out.println(minDepth.solution(node)); // 2
}
}