[LeetCode 题解]: Minimum Depth of Binary Tree
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.
题意很清楚,找到二叉树中深度最短的一条路径,DFS(貌似是面试宝典上的一道题)
类似的一道题:http://www.cnblogs.com/double-win/p/3737262.html
/**
* Definition for binary tree
* 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==NULL) return ;
if(root->left==NULL && root->right==NULL) return ; int Leftdepth = minDepth(root->left);
int Rightdepth = minDepth(root->right); if(Leftdepth==)
return Rightdepth+;
else if(Rightdepth==)
return Leftdepth+; return min(Leftdepth,Rightdepth)+;
}
};
[LeetCode 题解]: Minimum Depth of Binary Tree的更多相关文章
- 【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 ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- [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][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- [LeetCode 题解]: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 111 Minimum Depth of Binary Tree(DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- Java Servlet调用数据库复习
首先要导入jar包. 剩下的基本就是模版式的代码了: public class main { // JDBC 驱动名及数据库 URL static final String JDBC_DRIVER = ...
- ZooKeeper与仲裁模式
为了让服务器之间可以通信,服务器间需要一些联系信息.理论上,服务器可以使用多播来发现彼此,但我们想让ZooKeeper集合支持跨多个网 络而不是单个网络,这样就可以支持多个集合的情况. 为了完成这些, ...
- leetcode380
class RandomizedSet { public: /** Initialize your data structure here. */ RandomizedSet() { } /** In ...
- 微信小程序跳转navigateTo与redirectTo
转自:https://www.cnblogs.com/perfect-yuewei/p/8301761.html 2018-01-16 - 微信中跳转页面方法目前接触到两种 navigateTo与re ...
- 不同应用场景的10个Linux面试问题与解答
本文由 极客范 - 小道空空 翻译自 Avishek Kumar.欢迎加入极客翻译小组,同我们一道翻译与分享.转载请参见文章末尾处的要求. 这一次我们不再介绍某个特定主题的Linux面试问题,而是随机 ...
- java aop 日志打印 正则设置
package tz.lion.Utils.aop; import com.alibaba.fastjson.JSON;import org.springframework.web.multipart ...
- centos7的vsftpd的安装和配置
安装vsftpd的服务器相关软件 安装vsftpd服务器和客户端 sudo yum install ftp vsftpd 安装一个加密工具 sudo yum install libdb-utils.x ...
- 用Pylint规范化Python代码,附PyCharm配置
Pylint一个可以检查Python代码错误,执行代码规范的工具.它还可以对代码风格提出建议. 官网:https://pylint.readthedocs.io pip install pylint ...
- git的突出解决--git rebase之abort、continue、skip
(1)应用实例描述 假设在github或者gitoschina上建立了一个项目,默认分支为master分支,远程master分支上c.sh文件内容: 开发者A.B分别将项目拷贝到自己本地进行开发 某一 ...
- 设置WebBrowser内核渲染模式
前不久开发一个项目,是采用WebBrowser作为外壳,加载网页,由于网页是采用html5来进行开发的,当通过WebBrowser加载网页后,html5中的特性 都无法正常显示,而通过ie浏览器打开时 ...