[leetcode]_Minimum Depth of Binary Tree
第五道树题,10分钟之内一遍AC。做树题越来越有feel~
题目:求一棵树从root结点到叶子结点的最短路径。
思路:仍然是递归。如果一个结点的左右子树任意一边为Null,该子树的minDepth应为非null子树的高度+1;如果一个结点的左右子树两边都非Null,则该子树的minDepth应为两个子树的较小值
代码:
public int minDepth(TreeNode root) {
if(root == null) return 0;
if(root.left == null && root.right == null) return 1;
else if(root.left == null || root.right == null)
//如果该结点的left或者right为null,则该结点的depth应该为非null边子树高度 + 1
return minDepth(root.left) + minDepth(root.right) + 1;
else
//如果该结点的left和right都不等于null ,则该结点的depth应该为两边子树高度的较小值 + 1
return Math.min(minDepth(root.left) , minDepth(root.right)) + 1;
}
if语句的前两种情况本可以合并书写,考虑到合并起来理解性太差了,就这样了。第二个return 中,由于 肯定有一个minDepth的返回值是0,所以我就直接取它们的和值了,不分两种情况来写。
[leetcode]_Minimum Depth of Binary Tree的更多相关文章
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [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] 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: Minimum Depth of Binary Tree 解题报告
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode - Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 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:Minimus Depth of Binary Tree
The problem description: Given a binary tree, find its minimum depth. The minimum depth is the numbe ...
- [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 ...
随机推荐
- C++学习23 虚函数详解
虚函数对于多态具有决定性的作用,有虚函数才能构成多态.上节的例子中,你可能还未发现虚函数的用途,不妨来看下面的代码. #include <iostream> using namespace ...
- myBatis实例
一.搭建环境, 建立数据库 CREATE TABLE user( id ) not NULL AUTO_INCREMENT, userName ) DEFAULT NULL, userAge ) DE ...
- 朗逸2011款 1.4t 清除保养告警灯
朗逸2011款 1.4t 清除保养告警灯 Posted on 2015-03-01 21:06 编辑 仪表盘上有两个按钮 按住右边set键,钥匙旋转到通电状态,保持2s. 放掉set,按左边的切换按钮 ...
- Ext TreeGrid提交修改过的数据
本打算将整个treestore的数据提交到服务器,但找来找去没有找到好的方法,在翻api的时候发现了getUpdatedRecords()方法,拿来一试,试出此方法可以拿到被修改过的record so ...
- [POJ 1635] Subway tree systems (树哈希)
题目链接:http://poj.org/problem?id=1635 题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯. 我刚开始自己设计了哈希函数,不知道为什么有问题.. ...
- maven与git
================================================== maven的作用 ======================================== ...
- Rolling Cursor Invalidations with DBMS_STATS.AUTO_INVALIDATE (文档 ID 557661.1)
Rolling Cursor Invalidations with DBMS_STATS.AUTO_INVALIDATE (文档 ID 557661.1) 转到底部 In this Documen ...
- 用imageNamed加载图片产生的问题
通常我们会用imageNamed:来加载图片,但是用这个API有个问题,就是它会缓存加载的image. 因此,对于那些被重用的图片,这个API很高效.但是对于那些使用很少的图片,用这个就很耗内存,那怎 ...
- OC基础(1)
Objective-C简介 OC和C对比 第一个OC程序 面向对象思想 *:first-child { margin-top: 0 !important; } body > *:last-chi ...
- 高校应该使用 Drupal 的10大理由
使用 Drupal 已经成为全球顶尖高校中的一种潮流,它已经被全球数以百计的院校选择并应用,无论是哈佛.斯坦福.杜克.布朗.罗格斯.剑桥.耶鲁还是其它众多知名高校,都已经选择 Drupal 作为它们理 ...