LeetCode Balanced Binary Tree
来源:互联网 发布:德州seo 编辑:程序博客网 时间:2024/04/28 21:45
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.
Solution 1:
public class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
if (Math.abs(maxdeep(root.left) - maxdeep(root.right)) > 1) return false;
return isBalanced(root.left)&&isBalanced(root.right);
}
public int maxdeep(TreeNode root){
if (root == null) return 0;
return Math.max(maxdeep(root.left), maxdeep(root.right)) + 1;
}
}
Time Complexity: O(n^2)
Solution 2:
public class Solution {
public boolean isBalanced(TreeNode root) {
return BackOrderT(root) > -1;
}
public int BackOrderT(TreeNode root){
if (root == null) return 0;
int leftDep = BackOrderT(root.left);
int rightDep = BackOrderT(root.right);
if ( (Math.abs(leftDep - rightDep) > 1)||(leftDep == -1)||(rightDep == -1) ) return -1;
return Math.max(leftDep + 1, rightDep + 1);
}
}
Time Complexity: O(n)
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- [leetcode]Balanced Binary Tree
- Leetcode: Balanced Binary Tree
- LeetCode-Balanced Binary Tree
- LeetCode - Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- LeetCode | Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- Strcat,strcpy,strcmp,Strlen函数原型
- H.264的两个概念:DC系数和AC系数。 MV预测过程详解(附图)
- C/C++中内存区域划分大总结
- mpeg2,mpeg4,h264编码标准的异同
- http://www.cnblogs.com/ada-zheng/p/3284660.html
- LeetCode Balanced Binary Tree
- mplayer configure选项中文注释(MPlayer-1.0rc2)
- Android跳转到系统Activity关闭并返回原Activity的解决办法
- Largest Rectangle in Histogram
- 黑马程序员_ios基础总结5_C语言基本语法四
- 胖子哥的大数据之路(三)- 大数据仓库的需求分析该怎么做
- Bug 4742607 - CBC latch contention from concurrent index range scans (文档 ID 4742607.8)
- 【分享】深入浅出WPF全系列教程及源代码
- 9、SSH整合续2