题目

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.

分析

求二叉树的最小深度:根节点到最近叶子节点的路径长度。

同样采用递归的思想:

  1. 当根节点为空,返回0;
  2. 当根节点为唯一的二叉树节点时,返回1;
  3. 否则,求解并返回 最小(非空,保证最终到达叶节点)左右子树深度 + 1;

AC代码

class Solution {
public:
int minDepth(TreeNode* root) {
if (!root)
return 0;
//独立的根节点
else if (!root->left && !root->right)
return 1;
else{
int left_depth, right_depth;
if (root->left)
left_depth = minDepth(root->left);
else
left_depth = INT_MAX;
if (root->right)
right_depth = minDepth(root->right);
else
right_depth = INT_MAX;
return min(left_depth, right_depth) + 1;
} }
};

GitHub测试程序源码

LeetCode(111) Minimum Depth of Binary Tree的更多相关文章

  1. LeetCode(104) Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

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

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

  3. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  5. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  6. [LeetCode]题解(python):110 Balanced Binary Tree

    题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...

  7. LeetCode: Minimum Depth of Binary Tree 解题报告

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  8. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

  9. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

随机推荐

  1. collections 中 typing 中对象的引用

    from typing import ( Callable as Callable, Container as Container, Hashable as Hashable, Iterable as ...

  2. laravel使用swoole

    参考 参考2 另外主要用到artisan 首先创建SwooleCommand.php <?php namespace App\Console\Commands; use App\Http\Con ...

  3. (转)Unity3D中常用的数据结构总结与分析

    http://www.cnblogs.com/murongxiaopifu/p/4161648.html#array   1.几种常见的数据结构 常碰到的几种数据结构:Array,ArrayList, ...

  4. Android程序中使用iconfont心得

    1.关于iconfont iconfont既是icon又是font,具体来说应该是用font形式展现的icon.与传统图片格式的图标不同,这一种图标因为是以字体形式展现的,所以更改大小.颜色.背景颜色 ...

  5. 转:android 屏幕适配小结

    做android开发,开源嘛,满市场都是凌乱的机型,总少不了适配这样或那样的型号.在这里分享一下自己在开发中用到的方法. 首先要介绍一下drawable-mdpi.drawable-hdpi-1280 ...

  6. uvm_factory——我们的工厂(一)

    factory 机制是实现(功能):通过一个字符串来创建此字符串所代表的的类的一个实例. //----------------------------------------------------- ...

  7. -bash: mail: command not found

    近日,安装了一个最小化的centos 6.3 6,用mail发送邮件进行测试的时候提示-bash: mail: command not found mailx没有安装,于是: yum -y insta ...

  8. IOS之UI异步刷新

    NSOperationQueue     *operationQueue; // for rendering pages on second thread [operationQueue waitUn ...

  9. PHP-PHPExcel用法详解

    以下文章来源:diandian_520 http://blog.csdn.net/diandian_520/article/details/7827038 1.header header(" ...

  10. mysql IF语句使用

    类似于三元运算符 1)  IF(where,result1,result2) = where?result1:result2 例如 SELECT IF(1=1,1,2)    =>  1 2)  ...