[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 ...
随机推荐
- java程序内存使用
一.内存使用示意图 二.java运行时数据区域 1.程序计数器: 当前线程所执行字节码的行号提示器. 2.java虚拟机栈: 线程私有,与线程生命周期相同,保存基本数据类型,如果线程所请求的栈深度大于 ...
- [ActionScript 3.0] AS3.0 马赛克效果
var bmpd:BitmapData; var matrix:Matrix; var bmp:Bitmap; var size:Number = 5; /** * @author:Frost.Yen ...
- 问答精华-IntelliJ IDEA快捷键大全
这篇文章介绍了idea的默认快捷键http://www.jikexueyuan.com/blog/229.html 另外:老师将快捷键设置为eclipse的了,你需要在preference里面找到ke ...
- eclipse中代码注释
1.类注释 Window->Preference->Java->Code Style->Code Template alt+shift+J 默认的快捷键 或者 先敲“/”在敲两 ...
- C++ DLL中导出函数的声明的方法
定义: TESTDLLEXPORT_API int fnTestDllExport(void); TESTDLLEXPORT_API int fnTestCall(void); TESTDLLEXPO ...
- SQLAlchemy指南(tutorial)
对应版本: 0.3.4 目录 1 安装 1.1 安装SQLAlchemy 1.2 安装一个数据库API 2 快速开始 2.1 导入 2.2 连接到数据库 3 SQLAlchemy是两个库的包装 4 操 ...
- [SQL] 要查询9 月份的数据中的任意时间段,可能是一个月的,也可能是1日到15日的
SELECT * FROM [表名] WHERE datediff(month,[列名],
- Mingyang.net:Controller必需是public吗?
通常定义Controller时一般都定义成public: package net.mingyang.modules.system; @Controller @RequestMapping(" ...
- (转)C#精确时间计时器
原文地址:http://blog.sina.com.cn/s/blog_699d3f1b01012vgb.html 1 调用WIN API中的GetTickCount [DllImport(" ...
- 调用WEKA包进行kmeans聚类(java)
所用数据文件:data1.txt @RELATION data1 @ATTRIBUTE one REAL @ATTRIBUTE two REAL @DATA 0.184000 0.482000 0.1 ...