找出最短的从叶子到根的路径长

可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是10000000,毫无疑问这棵树肯定为空,因此在最后有(d>=1000000)?0:d;

 /**
* 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 depth(TreeNode* root){
if(!root) return ;
else if(!root->left && !root->right) return ;
else return min(depth(root->left),depth(root->right)) + ;
}
int minDepth(TreeNode* root) {
int d = depth(root);
return (d>=)?:d;
}
};

Leetcode 111 Minimum Depth of Binary Tree 二叉树的更多相关文章

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

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

  2. [LeetCode] 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 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

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

  4. LeetCode 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 ...

  5. (二叉树 BFS DFS) leetcode 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 ...

  6. [Leetcode] The minimum depth of binary tree二叉树的最小深度

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

  7. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  8. leetcode 111 Minimum Depth of Binary Tree ----- java

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

  9. Java [Leetcode 111]Minimum Depth of Binary Tree

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

随机推荐

  1. (转载)selenium-webdriver(python)

    转载地址: http://www.cnblogs.com/fnng/p/3183777.html 本节重点: 简单对象的定位 -----自动化测试的核心 对象的定位应该是自动化测试的核心,要想操作一个 ...

  2. nil、Nil、NULL和NSNull的区别和联系

    一.nil 我们给对象赋值时一般会使用object = nil,表示我想把这个对象释放掉: 或者对象由于某种原因,经过多次release,于是对象引用计数器为0了,系统将这块内存释放掉,这个时候这个对 ...

  3. Python:装饰器

    格式:在执行的参数前加上@functon 例1:传一个参数 #模拟验证功能 def login(auth): def fun(arg): #为了避免程序一执行的时候就执行验证,需要再加一层函数. pr ...

  4. [MOSEK] Mosek求解中遇到的奇葩内存问题

    在使用mosek优化库的时候,使用http://docs.mosek.com/7.0/capi/MSK_getxx_.html的 MSKrescodee MSK_getxx ( MSKtask_t t ...

  5. upgrade-php-5-1-to-php-5-3-using-yum-on-centos

    wget -q -O - http://www.atomicorp.com/installers/atomic | shyum upgrade phpyum -y remove atomic-rele ...

  6. jsp的9大对象

    1.requset对象 主要用于接受客户端通过HTTP协议传送给服务器端的数据     request.getProtocal()获得客户使用协议     request.getServletPath ...

  7. (Python) 安装、基本语法

    从今天起,我将开启python学习模式,并用博客记录学习的过程和相关知识点 1.Python下载安装 可以在官网:https://www.python.org/downloads/ 中下载各种版本的P ...

  8. MVC_Ajax请求

    MVC_Ajax请求MVC中的AJAX操作原理还是基于Jquery的封装操作.但是吧没有那么恐怖.Ajax.BeginForm:使用Ajax.BeginForm方法会生成一个form表单,最后以Aja ...

  9. MVC之校验

    MVC校验 首先要在Models中创建几个属性 例子:Id.UserName.Age属性.然后创建控制器,然后添加一个试图,选择强类型,选择支架模板Create生成页面,然后将所有控件改为TextBo ...

  10. eclipse中无法使用fat.jar

    因为某种需要,我要打jar包,而eclipse中自带的打包功能又太过于繁琐,因此找到这个插件.不过尝试了许久都没有成功,最后终于找到了原因,是因为该插件的版本太低的缘故.相见:https://code ...