-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (30 loc) · 815 Bytes
/
Solution.java
File metadata and controls
35 lines (30 loc) · 815 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
32
33
34
35
package MinimumDepthofBinaryTree;
import commons.datastructures.TreeNode;
/**
* User: Danyang
* Date: 1/27/2015
* Time: 15:11
*
* 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.
*/
public class Solution {
/**
* Notice:
* 1. must be leaf node
* @param root
* @return
*/
public int minDepth(TreeNode root) {
if(root==null)
return 0;
return getDepth(root, 1);
}
int getDepth(TreeNode cur, int depth) {
if(cur==null)
return Integer.MAX_VALUE;
if(cur.left==null && cur.right==null)
return depth;
return Math.min(getDepth(cur.left, depth+1), getDepth(cur.right, depth+1));
}
}