/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if(root==null)
return 0;
Queue<TreeNode> q=new LinkedList<TreeNode>();
q.offer(root);
int size=q.size();
int res=1;
int count=0;
while(!q.isEmpty())
{
TreeNode temp=q.poll();
if(temp.left==null&&temp.right==null)
{
//找到了叶子节点
break;
}
if(temp.left!=null)
q.offer(temp.left);
if(temp.right!=null)
q.offer(temp.right);
count++;
if(count==size)
{
count=0;
size=q.size();
res++;
}
}
return res;
}
}