首页 > 代码库 > 求树的最小深度

求树的最小深度

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    private int mins=10000;       public int minDepth(TreeNode root) {              if(root==null) return 0;      min(root,1);      return mins;                                    }      public void min(TreeNode t,int lev)   {       if(t==null) return;       if(t.right==null&&t.left==null)       {           if(lev<mins)mins=lev;       }         min(t.right,lev+1);         min(t.left,lev+1);                                          }                                }