forked from mengli/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumDepthofBinaryTree.java
More file actions
Latest commit
23 lines (20 loc) · 590 Bytes
/
Copy pathMinimumDepthofBinaryTree.java
File metadata and controls
23 lines (20 loc) · 590 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 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.
*/
publicclassMinimumDepthofBinaryTree {
publicintminDepth(TreeNoderoot) {
if (root == null)
return0;
if (root.left == null && root.right == null)
return1;
else {
intleftDepth = root.left != null ? minDepth(root.left)
: Integer.MAX_VALUE;
intrightDepth = root.right != null ? minDepth(root.right)
: Integer.MAX_VALUE;
returnMath.min(leftDepth, rightDepth) + 1;
}
}
}