题目:
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.
思路:
DFS,Recursion
Code:
int depth;
public int minDepth(TreeNode root) {
if(root==null) return 0;
depth=Integer.MAX_VALUE;
recurMin(1, root);
return depth;
}
public void recurMin(int dep, TreeNode root){
if(dep>=depth) return;
else if(root.left==null && root.right==null) depth=dep;
else{
if(root.left!=null) recurMin(dep+1,root.left);
if(root.right!=null) recurMin(dep+1, root.right);
}
}
备注:
Run Time 太长