LeetCode-111. Minimum Depth of Binary Tree

标签: LeetCode Tree
1人阅读 评论(0) 收藏 举报
分类:

Description

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.

Solution 1(C++)

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root) return 0;
        if(!root->right && !root->left) return 1;
        else if(root->right && root->left) return min(minDepth(root->right), minDepth(root->left))+1;
        else return 1 + (root->right?minDepth(root->right):minDepth(root->left));
    }
};

Solution 2(C++)

class Solution {
public:
    int minDepth(TreeNode* root){
        if(root == NULL)
                return 0;
        if(root->left == NULL && root->right == NULL)
            return 1;
        else
        {
            int l = minDepth(root->left);
            int r = minDepth(root->right);
            if(l == 0)
                l = r;
            if(r == 0)
                r = l;
            return min(l,r) + 1;
        }
    }
};

算法分析

挺简单的,就是要考虑全面,只有根节点的左右两个子节点都存在时才需要返回min(minDepth(root->right), minDepth(root->left))+1。

程序分析

查看评论

Minimum Depth of Binary Tree -- LeetCode

原题链接:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/  这道题是树的题目,其实跟Maximum Depth of Bi...
  • linhuanmars
  • linhuanmars
  • 2014-02-22 03:52:34
  • 14701

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

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

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

LeetCode(111)Minimum Depth of Binary Tree

题目如下: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th...
  • feliciafay
  • feliciafay
  • 2014-01-17 09:03:53
  • 1284

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

【111-Minimum Depth of Binary Tree(二叉树的最小深度)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tre...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-11 07:37:47
  • 3040

[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 s...
  • sbitswc
  • sbitswc
  • 2014-05-22 01:32:24
  • 6482

LeetCode 111 : Minimum Depth of Binary Tree (Java)

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

[LeetCode 111] Minimum Depth of Binary Tree

题目链接:minimum-depth-of-binary-tree /** * Given a binary tree, find its minimum depth. The min...
  • ever223
  • ever223
  • 2015-03-22 09:09:47
  • 961

LeetCode111——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 near...
  • booirror
  • booirror
  • 2015-02-05 15:23:26
  • 1628

leetcode-java-111. Minimum Depth of Binary Tree

/* 最小深度就是根节点到最近叶子节点的路径长度 和找二叉树的最大深度类似,但是区别如下: 递归终止条件: 1.根节点为null时,返回0 2.根节点左右都null时,返回1 3.大返回终止条件:返回...
  • github_34514750
  • github_34514750
  • 2016-08-24 10:33:19
  • 418
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 2521
    排名: 1万+
    文章存档
    最新评论