forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinDepth.java
More file actions
31 lines (29 loc) · 987 Bytes
/
MinDepth.java
File metadata and controls
31 lines (29 loc) · 987 Bytes
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
package normal; /**
* @program JavaBooks
* @description: 二叉树的最小深度
* @author: mf
* @create: 2019/10/18 23:04
*/
/**
* 给定一个二叉树,找出其最小深度。
* 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
*/
public class MinDepth {
public static void main(String[] args) {
int[] pre = {3, 9, 20, 15, 7};
int[] in = {9, 3, 15, 20, 7};
TreeNode node = TreeNode.setBinaryTree(pre, in);
int number = minDepth(node);
System.out.println(number);
}
private static int minDepth(TreeNode node) {
if (node == null) return 0;
if (node.left == null && node.right == null) return 1;
int left, right;
if (node.left != null) left = minDepth(node.left);
else left = Integer.MAX_VALUE;
if (node.right != null) right = minDepth(node.right);
else right = Integer.MAX_VALUE;
return Math.min(left, right) + 1;
}
}