【LeetCode练习题】Minimum Depth of Binary Tree
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.
和上一题对应,求二叉树的最小深度。
解题思路:
参考上一题Maximun Depth of Binary Tree中最后那个极短的解法。
另外需要判断一下递归返回0的时候的结果不可取,因为不是叶节点。
代码如下:
class Solution {
public:
int minDepth(TreeNode *root) {
if(!root)
return ;
int l = minDepth(root->left);
int r = minDepth(root->right);
if(l * r != )
return min(l,r)+;
else if(l == )
return r+;
else
return l+;
}
};
【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 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] 5. Minimum Depth of Binary Tree
二叉树基本功练习题,题目如下: Given a binary tree, find its minimum depth. The minimum depth is the number of node ...
- 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】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
随机推荐
- ora-28056 (Writing audit records to Windows Event Log failed)
系统:windows xp oracle 版本 SQL> select * from v$version; BANNER ------------------------------------ ...
- 后台数据导出为Excel
数据导出的方法如下: 一.下载office的类库:microsoft.office.interop.excel.zip 根据电脑安装的office版本选择引入相应的类库,office2007选择12. ...
- pyqt listwidget下面创建多张图片
def Photosvisi(self): i=0 self.lists.setIconSize(QtCore.QSize(70,70))#设置显示图片大小 self.lists.setResizeM ...
- Test注解的两个属性:expected和timeout
JUnit4:Test文档中的解释: The Test annotation supports two optional parameters. The first, expected, declar ...
- 小贝_mysql建表以及列属性
mysql建表以及列属性 简要: 一.建表原则 二.具体的列属性说明 一.建表原则 建表: 事实上就是声明列的过程,数据终于是以文件的形式放在硬盘(内存) 列: 不同的列类型占的空间不一样. 选列的原 ...
- 利用Python进行数据分析——数据规整化:清理、转换、合并、重塑(七)(1)
数据分析和建模方面的大量编程工作都是用在数据准备上的:载入.清理.转换以及重塑.有时候,存放在文件或数据库中的数据并不能满足你的数据处理应用的要求.很多人都选择使用通用编程语言(如Python.Per ...
- 关于js中select的简单操作,以及js前台计算,span简单操作
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- [spring入门学习笔记][spring的IoC原理]
什么叫IoC 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency ...
- FLASH图片上传功能—从百度编辑器UEditor里面提取出来
为了记录工作中碰到的各种问题,以及学习资料整理,今天开始,将以往的文章进行了一个整理,以后也开始认真的记录学习过程中的各种问题 在HTML里面的文件上传功能一直是个问题,为了实现上传文件大小限制,怎样 ...
- MVVM模式
MVVM的最大缺点貌似是,报错后不好找, 在安卓6.0的时候出现了一个工具叫做databinding,其中呢主要是用来帮助实现MVVM模式的快速开发 在使用databinding的时候我们需要做的 ...