Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长
可以回忆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 二叉树的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 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 ...
- (二叉树 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 ...
- [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 ...
- leetcode 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- 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 ...
- 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 ...
随机推荐
- Sublime text 3安装svn插件
这几天在研究sublime text 3的使用,感觉还不错,现在想让他能够支持svn,所以就写一下怎么安装svn插件吧~ 首先先说一下这个官方的插件网站 点我进入~ 进入之后,最上边的第一个就是点击安 ...
- C/C++语言的一些精简归纳
前言:本想直接写个关于OC语言,但觉得还是要说下C先. 先语言特性 C是面向过程的,没有类和对象概念,也就没有什么封装(这个?).继承.多态等特性. 而且是是中级语言,其编译过程包括:预编译(incl ...
- 【摘】Chrome解决高版本Stable Beta扩展程序强制停用问题
博客园插件遇到这个问题,下述摘自百度贴吧,原文地址http://tieba.baidu.com/p/3091171066 1]下载组策略模板 chrome.adm 2] Win+R gpedit.ms ...
- Xshell连接虚拟机
一般连接虚拟机失败 原因1:ip地址错误 当输入 ifconfig 只有lo没有eth0,或者有eth0,但eth0中确没有inet addr这一行 输入命令:dhclient eth0 就可以了
- @Html.Partials 加载分布视图传参数
如何在视图中利用 viewData参数和model参数,示例如下 <body> <div style="background:#ffd800;width:200px;pad ...
- SpringMVC配置入門
我的開發環境 開發工具: springsource-tool-suite-2.9.0 JDK版本: 1.6.0_29 tomcat版本:apache-tomcat-7.0.26 本文地址: ...
- poj 3613 经过k条边最短路 floyd+矩阵快速幂
http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路, ...
- codeforces 424D
题意:给定n,m<=300的矩阵,然后求一个长宽大于2的矩形,使得是从左上角位置开始逆时针绕边框一圈的时间最少.时间的计算是:给定3个数tu,tp, td,路径上数字增加为tu,相等为tp否则为 ...
- SQL语句汇总(终篇)—— 表联接与联接查询
上一篇博文说到相关子查询效率低下,那我们怎么能将不同表的信息一起查询出来呢?这就需要用到表联接. 和之前的UNION组合查询不同,UNION是将不同的表组合起来,也就是纵向联接,说白了就是竖着拼起来. ...
- redis和ssdb读取性能对比
最近关注了一下ssdb,它的特点是基于文件存储系统所以它支撑量大的数据而不因为内存的限制受取约束.从官网的测试报告来看其性能也非常出色和redis相当,因此可以使用它来代替redis来进行k-v数据业 ...