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.
记得要在合适的地方做正确的return。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int min = 9999;
public int minRec(TreeNode root){
if(root==null) return 0;
int l = minRec(root.left);
int r = minRec(root.right);
if(l==0) return r+1; //这个地方的Return我经常忘记
if(r==0) return l+1;
return Math.min(l, r) + 1;
}
public int minDepth(TreeNode root) {
return minRec(root);
}
}