111. Minimum Depth of Binary Tree

Posted 阿怪123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了111. Minimum Depth of Binary Tree相关的知识,希望对你有一定的参考价值。

/**
 * 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;
    }
}

 

以上是关于111. Minimum Depth of Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

111. Minimum Depth of Binary Tree

111.minimum depth of binary tree(E)

111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree

[LeetCode]题解(python):111 Minimum Depth of Binary Tree