方法一:(有待改进,方法中将minDepth这个本该为局部变量的参数设为成员变量,这样的设计不合理)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int minDepth = Integer.MAX_VALUE;
public int minDepth(TreeNode root) {
// int minDepth = Integer.MAX_VALUE;
return Depth(root, 1);
}
public int Depth(TreeNode root, int deep){
if(root == null) return 0;
if(root.left == null && root.right == null){
if(deep < minDepth) minDepth = deep;
}
if(root.left !=null)
Depth(root.left,deep+1);
if(root.right != null)
Depth(root.right,deep+1);
return minDepth;
}
}