【LeetCode】Minimum Depth of Binary Tree 解题报告
2015-09-17 11:01
471 查看
Minimum Depth of Binary Tree
[LeetCode]https://leetcode.com/problems/minimum-depth-of-binary-tree/
Total Accepted: 70767 Total Submissions: 243842 Difficulty: Easy
Question
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Examples
–Ways
运用递归,递归当前和 左子树和右子树的深度,某节点的左右子树都是空的时候,说明是叶子。计算根节点到此叶子的深度。注意:如果是叶子,那么此叶子的深度是1.
同时注意:如果有一方的某一子树为空,那么它的深度为0,但不应该进入树的深度的计算当中去。
Better solution:用HashMap存储已经遍历过的树,减少空间复杂度。实现效率的提高。
当root为空的时候直接返回0,因为MIN赋值很大,所以如果不单独预判的话会返回MIN
判断树的深度应该到叶子节点,也就是左右子结点都为空的那个结点
树的深度的根节点深度为1
Solution
托管在我的GitHub上:https://github.com/fuxuemingzhu/MinDepthOfTree
Captures
测试结果截图:采用 HashMap存储方法:
Reference
http://www.cnblogs.com/Antech/p/3705928.htmlDate
2015/9/17 10:49:04相关文章推荐
- Windows下Nginx的启动、停止等命令
- 职场35岁现象
- JDK原生类操作XML
- Service
- php与html 表单的结合
- 为什么要使用SLF4J而不是Log4J
- 筛选法求素数
- PHP生成二维码
- 防sql注入 php中get_magic_quotes_gpc配置
- 用Python开发自动化测试脚本
- 15_09_17 String 类型转换为 Long 类型
- Common mistakes when using Spring MVC
- sqoop从mysql向hive中导数据,报数据库访问被拒绝
- centos升级系统内核
- sqlite
- Fidder 用法
- 从外网访问局域网内的主机的方法(端口映射)
- C/C++刁钻问题各个击破之细说sizeof
- 【转】 关于ExpandableListView用法的一个简单小例子
- UVA 1665(并查集题目)