-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalancedBinaryTree_110.java
53 lines (46 loc) · 1.46 KB
/
BalancedBinaryTree_110.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/**
* My Understand OF Balanced Binary Tree is wrong
* the First is wrong!!!!! XXXXXXXXXXXXXXXXXXX
* 1. Find the longest, & shortest, then test whether the longest-shortest<=1;
* write recursion method directly
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
/**
* This is it's definition Time O(n^2), maxDepth is O(n), do it for each node => O(n^2)
*/
/*public class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 &&
isBalanced(root.left) && isBalanced(root.right) ;
}
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}*/
/**
* maxDepth recalculated for every node, can be merged in the isBalanced
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
return maxDepthBalanced(root) >= 0;
}
// -1, unbalanced, >=0 maxlength & balanced
public int maxDepthBalanced(TreeNode root) {
if (root == null) return 0;
int lb = maxDepthBalanced(root.left);
int rb = maxDepthBalanced(root.right);
if (lb < 0 || rb < 0 || Math.abs(lb-rb) > 1) return -1;
return Math.max(lb, rb) + 1;
}
}