题目:

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. POJ 3258 River Hopscotch(二分答案)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...

  2. 跟bWAPP学WEB安全(PHP代码)--PHP代码注入

    ---恢复内容开始--- 背景 今天我们换一个方式来分析这个漏洞,从渗透的角度去搞. 渗透过程 测试漏洞 先来看看,观察URL是:http://192.168.195.195/bWAPP/phpi.p ...

  3. 供安全工程师实用的SOC模型

    一.背景 如今,安全概念满天飞,什么安全运营中心(SOC).威胁情报(TI).态势感知等等不一而足,这些概念及其背后代表的安全思想都很好,不过很多产品为了迎合国内的工作汇报都做成了很多Dashboar ...

  4. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十五:FIFO储存模块(同步)

    实验十五:FIFO储存模块(同步) 笔者虽然在实验十四曾解释储存模块,而且也演示奇怪的家伙,但是实验十四只是一场游戏而已.至于实验十五,笔者会稍微严肃一点,手动建立有规格的储存模块,即同步FIFO.那 ...

  5. docker报错“net/http: TLS handshake timeout”的解决方法

    为了永久性保留更改,您可以修改 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值. { "registry-mirrors": ...

  6. 用AT命令调试调制解调器

    最早生产调制解调器的公司是贺氏,后来组建的厂家制造的调制解调器都与HAYES兼容,大部分的通信软件使用菜单来对调制解调器进行配置.检测.但是有些通信软件要求用户直接发命令给调制解调器,在这种情况下必须 ...

  7. AJAX之三种数据传输格式详解

    一.HTML HTML由一些普通文本组成.如果服务器通过XMLHTTPRequest发送HTML,文本将存储在responseText属性中. 从服务器端发送的HTML的代码在浏览器端不需要用Java ...

  8. Pandas的concat方法

    在此我用的concat作用是加入新的记录,存储数据来用过的,不知道数据量大时候,效率会怎样 # 使用pandas来保存数据 df1 = pd.DataFrame([poem], columns=['p ...

  9. 多线程情况下HashMap死循环的问题

    1.多线程put操作后,get操作导致死循环. 2.多线程put非null元素后,get操作得到null值. 3.多线程put操作,导致元素丢失. 死循环场景重现 下面我用一段简单的DEMO模拟Has ...

  10. TortoiseGit密钥的配置(转)

    add by zhj:说到密钥,就不得不提非对称加密.目前使用最广泛的非对称加密算法是rsa,它是美国三位科学家于1977年发明的. 一对密钥对有两个密钥,其中一个为私钥,一个为公钥,两者没有什么区别 ...