-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBalancedBinaryTree.java
More file actions
49 lines (45 loc) · 1.53 KB
/
BalancedBinaryTree.java
File metadata and controls
49 lines (45 loc) · 1.53 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class BalancedBinaryTree {
private static boolean isBalancedRecursive0(TreeNode root, int[] height) {
if (root == null) {
height[0] = 0;
return true;
}
int[] leftHeight = new int[]{0};
int[] rightHeight = new int[]{0};
boolean leftIsBalanced = isBalancedRecursive0(root.left, leftHeight);
boolean rightIsBalanced = isBalancedRecursive0(root.right, rightHeight);
height[0] = Math.max(leftHeight[0], rightHeight[0]) + 1;
if (!leftIsBalanced || !rightIsBalanced) {
return false;
} else {
return Math.abs(leftHeight[0] - rightHeight[0]) <= 1;
}
}
public boolean isBalanced0(TreeNode root) {
return isBalancedRecursive0(root, new int[]{0});
}
//Improved solution
private static int getHeightOfBalancedTree(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = getHeightOfBalancedTree(root.left);
int rightHeight = getHeightOfBalancedTree(root.right);
if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
return -1;
}
return Math.max(leftHeight, rightHeight) + 1;
}
public boolean isBalanced(TreeNode root) {
return (getHeightOfBalancedTree(root) != -1);
}
}