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 shortest path from the root node down to the nearest leaf node.
题目标题:Tree
这道题目给了我们一个二叉树,让我们找到它最小的深度,注意这里的最小深度是指root 到一个 leaf node。
举个例子: 1
/
2
/
3
这种情况,最小depth 是3, 而不是因为1的right 是null ,最小depth 就等于1了。
和求最大深度差不多,每次遇到null node 返回0。接着每一个点返回 left 和right 中小的那一个depth + 1。但是我们需要控制住一个情况,就是如果parent 的left 和right 其中有一个返回的是0, 意思是有一个null 点, 那么这里就不能取两个child 中小的那个了,因为一个点是null, 另一个不是,那么就说明,null点不是leaf 点。 这种情况只需要返回那个不是null 的点的 depth + 1 就可以了。
Java Solution:
Runtime beats 17.13%
完成日期:07/03/2017
关键词:Tree
关键点:设条件控制住left 和right 其中一个点是null 的这种情况,另外返回其他值
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public int minDepth(TreeNode root)
{
if(root == null)
return 0; int left_depth = minDepth(root.left);
int right_depth = minDepth(root.right); if(left_depth == 0 && right_depth != 0)
return right_depth + 1;
else if(left_depth != 0 && right_depth == 0)
return left_depth + 1;
else
return Math.min(left_depth, right_depth) + 1;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
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 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- (二叉树 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] 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 ...
随机推荐
- Jacoco远程统计tomcat服务(Windows系统)的代码覆盖率
Jacoco远程统计tomcat服务(Windows系统)的代码覆盖率 2017-09-21 目录 1 Jacoco的安装和设置 1.1 什么是Jacoco? 1.2 Jacoco安装 1.3 ...
- spring 面向切面(AOP)
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP与OOP是面向不同领域的两种设计思想. ...
- AngularJS - 依赖注入(Dependency Injection)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 依赖注入 依赖注入是软件设计模式中的一部分,用于处理组件是如何得到它说依赖的其它组件的. ...
- 再探Spring IOC
这次做了提纲 下面再来一个case study case描述: 这是工具类 //bean的配置信息略去 class MyUtil{ private static UserDao userDao; p ...
- mongoDB学习手记1--Windows系统下的安装与启动
第一步:下载安装包 我们首先需要下载 mongodb 的安装包,直接到官网下载即可.地址为:https://www.mongodb.com/download-center#community. 看下自 ...
- JSP入门 el表达式
我们已经知道el是jsp-2.0规范的一部分,tomcat-5.x版本以上都已经能够支持jsp-2.0规范,但在更低版本的tomcat和webphere,weblogic中还是无法使用这一便捷方式. ...
- 通过SQL脚本导入数据到不同数据库避免重复导入三种方式
前言 无论何种语言,一旦看见代码中有重复性的代码则想到封装来复用,在SQL同样如此,若我们没有界面来维护而且需要经常进行的操作,我们会写脚本避免下次又得重新写一遍,但是这其中就涉及到一个问题,这个问题 ...
- 最长上升子序列 LIS(Longest Increasing Subsequence)
引出: 问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…< ...
- 如何创建一个Django项目
Django 软件框架 软件框架是由其中的各个模块组成,每个模块负责特定的功能,模块与模块之间相互协作来完成软件开发. MVC简介 MVC框架的核心思想是:解耦,让不同的代码块之间降低耦合,增强代码的 ...
- Web API 路由 [一] Convention-Based Routing
Routing by Naming Convention 在App_Start/ WebApiConfig.cs文件中 routes.MapHttpRoute( name: "API Def ...