/
_0111_MinimumDepthOfBinaryTree.java
46 lines (40 loc) · 1.46 KB
/
_0111_MinimumDepthOfBinaryTree.java
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
package com.diguage.algorithm.leetcode;
import com.diguage.algorithm.util.TreeNode;
import java.util.Objects;
import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
import static java.util.Arrays.asList;
/**
* = 111. Minimum Depth of Binary Tree
*
* https://leetcode.com/problems/minimum-depth-of-binary-tree/[Minimum Depth of Binary Tree - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-02-07 21:26
*/
public class _0111_MinimumDepthOfBinaryTree {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Minimum Depth of Binary Tree.
* Memory Usage: 40.1 MB, less than 48.44% of Java online submissions for Minimum Depth of Binary Tree.
*/
public int minDepth(TreeNode root) {
if (Objects.isNull(root)) {
return 0;
}
if (Objects.isNull(root.left) && Objects.isNull(root.right)) {
return 1;
}
int min = Integer.MAX_VALUE;
if (Objects.nonNull(root.left)) {
min = Math.min(minDepth(root.left), min);
}
if (Objects.nonNull(root.right)) {
min = Math.min(minDepth(root.right), min);
}
return min + 1;
}
public static void main(String[] args) {
_0111_MinimumDepthOfBinaryTree solution = new _0111_MinimumDepthOfBinaryTree();
int r1 = solution.minDepth(buildTree(asList(3, 9, 20, null, null, 15, 7)));
System.out.println((r1 == 2) + " : " + r1);
}
}