[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 ...
随机推荐
- php502故障处理
一次打开网站,发现502,第一反应肯定是php-fpm没启动,尝试启动还是502. 1.首先查询Nginx日志发现如下连接PHP失败: 2016/07/29 15:56:04 [error] 2376 ...
- firefox 插件 取消认证签名
Firebug Tab Mix plus :系统退出自动保存tab List. tab mix options>Session>start/exit>when browse ...
- ios常用空间UIScrollViewIndicator的一些属性
UIScrollView属性: 1 alwaysBounceHorizontal BOOL值,当水平滚条到达终点,总是(视图)弹跳 2 alwaysBounceVertical ...
- axis2 webService开发指南(1)
参考文件:blog.csdn.net/IBM_hoojo http://hoojo.cnblogs.com/ 1 WebService简介 WebService让一个程序可以透明的调用互联网的程序,不 ...
- Bresenham画线算法
[Bresenham画线算法] Bresenham是一种光栅化算法.不仅可以用于画线,也可以用用画圆及其它曲线. 通过lower与upper的差,可以知道哪一个点更接近线段: 参考:<计算机图形 ...
- Unity代码里的Position和界面上的Position
代码里的Position = 世界坐标 this.gameObject.transform.position 界面上的Position = localPosition
- 4418开发板读取u盘说明
1.插上u盘后会在dev下生成两个文件db db1 将db1挂载即可访问..
- code1052 地鼠游戏
贪心算法,从后往前 来自codevs的题解: 我的纠结思考过程:如果每一秒都没有重复的地鼠出现 那么肯定是一个一个挨着打如果有重复的地鼠 那么要考虑打那个更优 当然是选分值最大的 单纯这样想很合理 但 ...
- linux进入单用户方法-乾颐堂
RedHat7.2 在使用GRUB引导程序的时候如何进入单用户 1.在出现GURB引导画面时,按字母e,进入GRUB编辑状态 2在引导菜单后添加“1”或single,选定它,然后按字母b,就可引导到单 ...
- Python使用日常
#Python中文件夹和文件的判断import os My_Path = "/home/lpworkstudy/Gooddir/" #现在我们判断这个文件夹是否存在 #如果不存在, ...