您的位置:首页 > 其它

【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.html

Date

2015/9/17 10:49:04
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: