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.
直接将最深二叉树的代码稍微改下,是错的,分析特定例子,当遇到有结点为NULL时就得要结束了
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode p) {
int leftTreeDepth,rightTreeDepth;
if(p==null)return 0;
leftTreeDepth=minDepth(p.left);
rightTreeDepth=minDepth(p.right);// if(leftTreeDepth== 0 && rightTreeDepth ==0) return 1;
// if(leftTreeDepth==0)leftTreeDepth=Integer.MAX_VALUE;
// if(rightTreeDepth==0)rightTreeDepth= Integer.MAX_VALUE;
if(leftTreeDepth==0) return rightTreeDepth+1;
if(rightTreeDepth==0) return leftTreeDepth+1; return leftTreeDepth<rightTreeDepth?leftTreeDepth+1:rightTreeDepth+1;
}
}