-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalancedBinaryTree.java
60 lines (50 loc) · 1.51 KB
/
BalancedBinaryTree.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
54
55
56
57
58
59
60
package org.sean.tree;
/***
* 110. Balanced Binary Tree
*/
public class BalancedBinaryTree {
private int height(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return Math.max(height(root.left), height(root.right)) + 1;
}
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
// time: O(N*logN)
boolean left = isBalanced(root.left);
boolean right = isBalanced(root.right);
boolean result = Math.abs(height(root.left) - height(root.right)) <= 1;
return left && right && result;
}
// ====================================================================================
private int checkHeight(TreeNode root) {
if(root == null)
return 0;
int leftHeight = checkHeight(root.left);
if(leftHeight == -1) {
return -1; // not balanced
}
int rightHeight = checkHeight(root.right);
if(rightHeight == -1) {
return -1;
}
int diff = leftHeight - rightHeight;
if(Math.abs(diff) > 1) {
return -1;
}else {
// return the height
return Math.max(leftHeight, rightHeight) + 1;
}
}
public boolean isBalanced2(TreeNode root) {
// time: O(N)
// space: O(H), H : height of the tree
return (checkHeight(root) != -1);
}
}