leetcode111_二叉树最小深度

public int minDepth(TreeNode root) {
    int depth = 0;
    if(root == null) return depth;
    Deque<TreeNode> q = new LinkedList();
    q.offer(root);
    while(!q.isEmpty()) {
        int size = q.size();
        depth++;
        for(int i = 0; i < size; i++) {
            TreeNode node = q.poll();
            if(node.left == null && node.right == null) return depth;
            if(node.left != null) q.offer(node.left);
            if(node.right != null) q.offer(node.right);
        }
    }
    return depth;
}
posted @ 2022-02-18 22:36  明卿册  阅读(16)  评论(0)    收藏  举报