LeetCode笔记:111. Minimum Depth of Binary Tree

问题:

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

大意:

给出一个二叉树,找到他最小的深度。

最小的深度是指从根节点到叶子节点距离最短的节点数。

思路:

这道题要找到最短的深度,个人认为更应该用广度优先遍历来做,一层一层地扫过去,哪一层有叶子节点了,就不扫了,把深度返回来。如果用深度优先遍历,每一条路都要走完,要所有路径都走到最后才能确定最短的。

广度优先取一个队列来记录每一层的节点数,利用队列先进先出的特性,一层层扫,每次扫到下一层的都加到队尾,把当前层的个数扫完后就将层数加一,直到找到叶子节点就直接返回当前找的深度。

代码(Java):

/**
 * 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) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if (root == null) return 0;

        int result = 1;
        queue.offer(root);
        while (!queue.isEmpty()) {
            int levelNum = queue.size();
            for (int i = 0; i < levelNum; i++) {
                if (queue.peek().left == null && queue.peek().right == null) return result;

                if (queue.peek().left != null) queue.offer(queue.peek().left);
                if (queue.peek().right != null) queue.offer(queue.peek().right);
                queue.poll();
            }
            result++;
        }
        return result;
    }
}

合集:https://github.com/Cloudox/LeetCode-Record
版权所有:http://blog.csdn.net/cloudox_

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值