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

可以回忆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. ios app打ipa包

    在app上传到appstore之前,我们通常要经过打包测试的步骤,导出到testFlight中.或者其他第三方的平台里面进行测试,这时候就需要打包成ipa包导出.好了,废话不多说,上图.步骤如下: 1 ...

  2. OA项目之分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Digit ...

  3. C# WebClient 实现上传下载网络资源

    下载数据 WebClient wc = new WebClient();1 string str= wc.DownloadString("地址")://直接下载字符串 2 wc.D ...

  4. php数组函数

    1.键值函数 array_values()返回数组元素值,组成一个新的索引数组 2.array_keys()返回数组所有键名,组成一个索引数组 3.in_array()检查数组中是否存在某个值 4.a ...

  5. HLSL之镜面反射光照

    今天学习的是镜面的反射光照,其实一般在场景中基本环境光和漫反射光照已经可以表现出一个不错的照明了,今天的镜面反射光照其实仅仅适合于需要在表面添加抛光或者闪耀的物体上,例如金属.玻璃等等,同时也是基于之 ...

  6. Github心得体会

    Github是一个代码托管的网站,以前端的代码为主,还有很多互动.​ 在我的理解看来,github并不仅仅是一个代码库,你可以自由注册,推送自己一些感兴趣编写的源代码.它不是单纯的保存代码,更多的是让 ...

  7. javascript 对象属性的get set访问器写法

    function Person() {     var age = new Date().getFullYear() - 18;     Object.defineProperty(this, &qu ...

  8. Java 第14章 字符串

    字符串 基本数据类型和引用数据类型作为方法参数 ,在传递时有什么不同之处. 答:基本数据类型按值传递,相当于复制了一份过去. 引用数据类型是指向引用 内存地址,两个地方 根据地址使用同一份数据,如被更 ...

  9. 在NopCommerce中启用MiniProfiler

    MVC MiniProfiler是Stack Overflow团队设计的一款对ASP.NET MVC.WebForm 以及WCF 的性能分析的小程序.可以对一个页面本身,及该页面通过直接引用.Ajax ...

  10. Java里this的作用和用法

    this, 一个官方的说法是,this首先是一个对象,它代表调用这个函数的对象. 根据面向对象的基本语法,每当调用变量或者函数的时候,都要按照类名.变量(函数)的格式来调用,意即每个变量或函数都必须属 ...