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.递归

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root)
return ;
else if(root->left && !root->right)
return +minDepth(root->left);
else if(!root->left && root->right)
return +minDepth(root->right);
else
return +min(minDepth(root->right),minDepth(root->left));
}
};

2.栈,待续

【LeetCode】111 - Minimum Depth of Binary Tree的更多相关文章

  1. 【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 ...

  2. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

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

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

  4. 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

    求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  5. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  6. 【leetcode❤python】 111. Minimum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  7. LeetCode OJ 111. Minimum Depth of Binary Tree

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

  8. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  9. 【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 long ...

随机推荐

  1. Android 使用Application类保存应用的全局数据

    在实际应用我们经常需要对数据进行交互与保存,但Intent中默认的方法对传输数据是有类型限制的,当我们需要传输或保存一个复杂的泛型数据时,使用Application是一个很好的解决办法. 顾名思义,A ...

  2. wso2 data services返回json数据方法

    一.首先要修改下配置文件. 修改\repository\conf\axis2目录下axis2.xml与axis2_client.xml配置文件. 增加<parameter name=" ...

  3. JSP的执行过程及其异常处理机制

    1.JSP的执行过程     虽然JSP感觉上很像一般的HTML网页,但事实上它是以Servlet的形式被运行的.因为JSP文件在第一次运行的时候会先解释成Servlet源文件,然后编译成Servle ...

  4. JAVASCRIPT和JQUERY判断浏览器信息总汇(备忘)

    <script type="text/javascript">        //jquery判断浏览器信息        $(function(){          ...

  5. Eclipse无法通过FileExplore打开真机data目录

    ok ——> Eclipse无法通过FileExplore打开真机data目录 ref:http://blog.csdn.net/koyoter/article/details/7657440 ...

  6. c# 获取数组中最大数的值

    求数组中最大的数的值:1.数组的max函数: class Program { static void Main(string[] args) { ,,,,,,,,,}; int max= GetMax ...

  7. 2-Medium下的MultipleCommandAssembly

    1.首先创建一个控制台项目 2.添加引用[红色的都是将项目添加为引用,其中蓝色的log4net是直接从源代码下的Reference文件夹下引用的dll] 3.写代码 1)首先将config配置好[ht ...

  8. [Golang]实习最后一天小纪念+并发爬虫小练习

    今天是我在公司实习的最后一天,一个月的时间真的是太短暂了,我非常享受在公司工作的这一个月,在这里Leader和同事们对我的帮助极大地促进了我技术水平的进步和自信心的提升,我发自内心地感谢白山云科技给我 ...

  9. [HDOJ5667]Sequence(矩阵快速幂,费马小定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5667 费马小定理: 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p). 即 ...

  10. 简单理解Hibernate三种状态的概念及互相转化

    本文描述了Hibernate三种状态的概念及互相转化.Java对象的生命周期中有三种状态,而且互相转化.它们分别是临时状态,持久化状态,以及游离状态. AD:WOT2015 互联网运维与开发者大会 热 ...