题目:

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.

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

分析:

查找二叉树的最小深度

分2种情况:

如果结点为NULL,返回0

否则,返回1+左右结点的最小深度

但这里有个地方需要注意,如果左右结点有一个为空,则应该返回1+另一个不为空的深度

如果左右节点都为空直接返回1

总结一下就是

if(l==0 || r==0)
return 1+l+r;

AC代码

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

LeetCode-MinimumDepthOfBinaryTree的更多相关文章

  1. leetcode — minimum-depth-of-binary-tree

    /** * Source : https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ * * * Given a binary t ...

  2. [牛客网 -leetcode在线编程 -02] minimum-depth-of-binary-tree -树的最短深度

    题目描述 题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along ...

  3. minimum-depth-of-binary-tree leetcode C++

    Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short ...

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

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  6. 【LeetCode OJ】Minimum Depth of Binary Tree

    Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...

  7. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

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

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

  9. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

  10. Minimum Depth of Binary Tree ——LeetCode

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. HDU 4004 The Frog's Games(二分答案)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  2. .net的XML对象序列化VS WCF中xml序列化问题

    整理一下 .net 对象序列化注意事项: 1. 字段:必须是 public类型 2.属性:只读或者只写的属性不被序列化,只有 可读可写并且赋值的才可以 序列化: Someclass obj = new ...

  3. 《代码大全》阅读笔记-33-个人性格(personal character)

    很多好的编程做法都能减轻你的大脑灰质细胞(指脑力)的负担. 将系统"分解",是为了使之易于理解("设计的层次"). 进行审查.评审和测试正是为了减少人为失误.如 ...

  4. jenkins - svn: E170001报错的原因以及解决方案

    1. 什么问题What? 使用Jenkins配置的svn拉取项目,Jenkins报错:svn: E170001; Your credentials to connect to the reposito ...

  5. SIM900A基站定位调试笔记 -转

    第1步:ATE1 握手并设置回显 第2步:AT+CGMR 查看SIM900的版本信号 第3步:AT+CSQ 查看信号质量 第4步:AT+CREG? 查看GSM是否注册成功 第5步:AT+CGREG?  ...

  6. [SQL] 用SQL语句检查CPU和磁盘空间

    在MS Sql Server中可以能过以下的方法查询出磁盘空间的使用情况及各数据库数据文件及日志文件的大小及使用利用率: 1.查询各个磁盘分区的剩余空间:Exec master.dbo.xp_fixe ...

  7. 修改nose_html_reporting,解决输出带中文时,不能生成html文件

    在使用nose_html_reporting时,如果测试输出中带有中文,那么html输出会失败,提示如下: 提示'ascii'编码码失败 这是因为在string.IO中取回来的数据与当前脚本中声明的编 ...

  8. Redis+Keepalived实现高可用

    使用redis哨兵可以在主服务器出现故障的时候自动切换主从,但是从服务器的IP不同于原主服务器的IP还需要在客户端手动修改IP才能生效 下面使用keepalived实现VIP自动漂移 keepaliv ...

  9. CodeForces 551C - GukiZ hates Boxes - [二分+贪心]

    题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...

  10. 0003python中的可变参数

    >>>def foo(x,y,z,*args,**kargs): print x print y print z print args print kargs >>> ...