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 ...
随机推荐
- node.js 基础学习 express安装使用
安装好nodeJs,我们需要使用命令行中安装express. 我这里默认将Node.js安装在C:\Program Files\nodCejs\盘中. 在保持联网的状态下,依次输入如下命令. npm ...
- IE、FF、Safari、OP不同浏览器兼容报告
IE.FF.Safari.OP不同浏览器兼容报告 1 浏览器内核简介 Trident IE浏览器(GreenBrowser绿色浏览器, 遨游浏览器....都是IE) Geckos Fi ...
- POJ2309 -- BST
找找规律,实际上是二分查找的过程,只要找到了mid与输入的n相同的话,直接输出left和right就可以了. 代码如下: #include <iostream> using namespa ...
- 我发现调用boostrap的弹框
在引用了boostrap.js和boostrap.css之后 本来boostrap是基于jQuery的.但是我们的项目里没有用jQuery,而是用的zepto. 调用boostrap的弹框有两种方式: ...
- 第七章 springboot + retrofit
retrofit:一套RESTful架构的Android(Java)客户端实现. 好处: 基于注解 提供JSON to POJO,POJO to JSON,网络请求(POST,GET,PUT,DELE ...
- equals(==),toString
equals/==: 只有指向同一个对象是,才返回true. 特殊: String s1 = new String("hello"); String s2 = new String ...
- MySQL~ IN , FIND_IN_SET , LIKE
MySQL- IN , FIND_IN_SET , LIKE SELECT * FROM test where area IN (1, 2, 3); SELECT * FROM test where ...
- linux下进度条的编写和实现
实现了一个简单的进度条,主要技术啥的算不上,但有几个需要注意的点 首先是回车符,回车符可不是\n,我们可以把\n看成是两个动作的合体,分别是,回车和换行,都有自己对应的符号,这利用回车符一直在同一个位 ...
- Makefile技术和应用总结
如何学习和运用Makefile 怎么写Makefile?不想讲太多如何如何做.Makefile这东西,公司让一两个人来负责就好了,否则一定是一锅粥.每次看到招聘广告里说,要求懂Makefile,懂Li ...
- android listview用adapter.notifyDataSetChanged()无法刷新每项的图标
http://blog.csdn.net/caizhegnhao/article/details/41318575 今天在开发中遇到一个很奇怪的listview的问题. 这个问题情景是我的应用需要做一 ...