题目

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. linux下curl get方法传递参数

    例如 url 为 http://mywebsite.com/index.php?a=1&b=2&c=3 web形式下访问url地址,使用$_GET是可以获取到所有的参数 然而在linu ...

  2. how browser supports https

    1. pre-installed certificate authorities 2. ssl/tls encription ssl/tls handshake flow: 1. exchange d ...

  3. 转 OUI and OPatch Do Not Recognize JDK/JRE on Windows

    issue 1: 新覆盖的opatch 提示,无法opatch 报错 此时不应有1.6 D:\app\Administrator\product\11.2.0\dbhome_1\OPatch\ocm\ ...

  4. 转 PHP函数---$_Get()和$_Post()的用法

    一.$_Get()和$_Post()函数是用来传值的,即对应两种提交表单的方法,get和post. 二.$_Get方法 (1)获取通过URL的传值 Example 1 新建两个PHP文件,1.php, ...

  5. D. Anton and Chess 模拟题 + 读题

    http://codeforces.com/contest/734/problem/D 一开始的时候看不懂题目,以为象是中国象棋那样走,然后看不懂样例. 原来是走对角线的,长知识了. 所以我们就知道, ...

  6. (转载)Unity 优化总结

    Unity 优化总结 2017-03-10 | 发布 大海明月  zengfeng75@qq.com  | 分类 Unity  | 标签 Unity  优化 相关文档 UGUI 降低填充率技巧两则 U ...

  7. Java微服务 进程间通信

    目录 进程间通信 同步调用 异步调用 实现方式 进程间通信 同步调用 同步调用比较简单,一致性强,但是容易出调用问题,出现单点故障,因为之间相互依赖,比如RPC必须要依赖的模块上线可用,己方才能调用, ...

  8. jQuery测试及解析

    解析:下标从0开始 解析:最大119  解析:鼠标移过mouseover  解析:  var   变量值=变量名

  9. Spring Cloud Config git版

    由于在学习这块内容的时候还不会使用gitHub所以就用了osc的码云 config server POM文件 <dependency> <groupId>org.springf ...

  10. Mybatis中的复合条件查询

    1.Map中根据字段名存儲: 定义接口:List<Student> selectByCondition1(Map<String,Object> map); 映射文件: < ...