【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution
[LeetCode]
https://leetcode.com/submissions/detail/40087813/
Total Accepted: 72203 Total Submissions: 225370 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.
https://leetcode.com/submissions/detail/40087813/
Examples
–
Ways
运用递归,递归当前和 左子树和右子树的深度,当所有的左右子树的深度相差不超过1的时候,就是平衡二叉树。
Better solution: 每次计算树的深度的时候保存下来树的深度到一个HashMap里,下次寻找的时候先到 HashMap 里查找,找不到在进行计算,然后继续保存。优化算法后,是算法复杂度降到了O(n)。
Solution
托管在我的GitHub上:
https://github.com/fuxuemingzhu/BalancedTreePro
Captures
测试结果截图:
Reference
这个帮助对我很大:
http://www.cnblogs.com/Antech/p/3705928.html
Date
2015/9/17 0:50:10