关闭

[leetcode]111.Minimum Depth of Binary Tree

标签: leetcode
344人阅读 评论(0) 收藏 举报
分类:

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.

题目链接:111. Minimum Depth of Binary Tree

这道题可以用递归或循环解决。下面给出两种方法的代码。
对于循环方法来说,我们用BFS解决这道题并不用遍历整个树,而是当遇到第一个叶节点时就可以停止了。因为BFS广度优先遍历是从根开始向远的地方遍历的。那么遇到的第一个叶节点的深度显然也就是我们需要的最小深度。】
具体分析请看代码中的注释。

其中iterative method的时间复杂度为O(2n-1),空间复杂度为O(2n1),n为二叉树深度。
recursive method的时间复杂度为(复习一下CRLS再算。)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    /*
    iterative method
    */
    public int minDepth(TreeNode root){
        if (root == null){
            return 0;
        }
        //BFS
        //by BFS, we only need to consider about the first leaf node we encountered. Based on the mechanism of BFS, the first leaf node we encountered is the least deep leaf node in the tree. So we end the search as soon as we reach this leaf node. 
        Queue<TreeNode> nodes = new LinkedList<>();
        int depth = 0;
        nodes.offer(root);
        while (!nodes.isEmpty()){
            //this size is the current number of nodes in the queue. 
            //it makes use of the feature that at the begining of every iteration, only tree nodes of the same depth are stored in the tree. So we need to iterate "size" times in the later for loop to poll out all the nodes of the given depth while only increase depth for 1. 
            int size = nodes.size(); 
            ++depth;
            for (int i = 0; i < size; i ++){
                TreeNode current = nodes.poll();
                if (current.left == null & current.right == null){
                    //if current node is a leaf node, we end the BFS, cause this is the minimun depth we need. 
                    return depth;
                }
                if (current.left != null){
                    nodes.offer(current.left);
                }
                if (current.right != null){
                    nodes.offer(current.right);
                }
            }

        }

        return depth;
    }

    /*
    recursive method
    */
    public int minDepth(TreeNode root) {
        if (root == null){
            return 0;
        }
        if (root.left != null && root.right != null){
            //if the current root we are considering have both left and right child, then we will choose the one with the smaller depth. 
            return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
        }
        else{
            //if the current root we are considering have only 1 child or no child
            //1 child: we choose the one with bigger depth, cause we are considering the leaf node, so if there is only one child, we need to go deeper on that node.
            //no child, no matter which child we choose, the result is the same, it will return 0 + 1.
            return Math.max(minDepth(root.left), minDepth(root.right)) + 1;
        }
    }
}
0
0
查看评论

Minimum Depth of Binary Tree -- LeetCode

原题链接:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/  这道题是树的题目,其实跟Maximum Depth of Binary Tree非常类似,只是这道题因为是判断最小深度,所以必须增加一个叶子的判断(因为...
  • linhuanmars
  • linhuanmars
  • 2014-02-22 03:52
  • 14260

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution Given a binary tree, find its maxi...
  • wangyaninglm
  • wangyaninglm
  • 2015-05-13 22:16
  • 3787

Maximum Depth of Binary Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/merge-two-sorted-lists/  这道题目比较简单,经典的链表基本操作。维护两个指针对应两个链表,因为一般会以一条链表为基准,比如说l1, 那么如果l1当期那的元素比较小,那么直接移动l1...
  • linhuanmars
  • linhuanmars
  • 2014-02-22 03:34
  • 12703

【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

【104-Maximum Depth of Binary Tree(二叉树的最大深度)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree, find its maximum depth.   The maximum depth is...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-08 06:43
  • 2949

111. Minimum Depth of Binary Tree [easy] (Python)

题目链接https://leetcode.com/problems/minimum-depth-of-binary-tree/题目原文 Given a binary tree, find its minimum depth. The minimum depth is the numbe...
  • coder_orz
  • coder_orz
  • 2016-05-07 13:43
  • 1188

【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree

今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的,因为c++版的代码网上比较多,所以就只分享一下Python的代码吧,刚学完Python的基本语法,做做LeetCode的题目还是不错的,对以后找工作面试也有帮助! 刚开始就从AC率最高的入手吧! ...
  • u011613729
  • u011613729
  • 2014-07-20 13:18
  • 3634

LeetCode 111 : Minimum Depth of Binary Tree (Java)

解题思路:找最小要比找最大复杂,因为递归对于最大来说如果一个节点只有左子树或右子树,它无需额外考虑,仍可以一时同仁对左右子树调用递归,只不过对null的子树返回的是0,而在比较取较大者时会自动放弃较小的0值。但对于最小来说,比较的时候是取较小者,不能对null的子树返回0,否则取较小的时候会取该子树...
  • changetocs
  • changetocs
  • 2015-12-01 08:19
  • 457

LeetCode104——Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthes...
  • booirror
  • booirror
  • 2015-02-05 15:29
  • 1528

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 ...
  • fly_yr
  • fly_yr
  • 2015-10-18 13:09
  • 1631

[LeetCode-55]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 neares...
  • sbitswc
  • sbitswc
  • 2014-05-22 01:32
  • 6364
    个人资料
    • 访问:94385次
    • 积分:1514
    • 等级:
    • 排名:千里之外
    • 原创:84篇
    • 转载:1篇
    • 译文:4篇
    • 评论:6条
    文章分类
    我的GitHub