-
Notifications
You must be signed in to change notification settings - Fork 2
/
InterviewQuestion4.java
59 lines (48 loc) · 1.51 KB
/
InterviewQuestion4.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
package id.dondon.ctci.chapter4;
/**
* Check Balanced
* Implement a function to check if a binary tree is balanced.
* For the purposes of this question, a balanced tree is defined to be a tree such that
* the heights of the two subtrees of any node never differ by more than one.
* */
public class InterviewQuestion4 {
/**
* Brute force method
* */
public static boolean solution1(TreeNode root) {
if (root == null) {
return true;
}
int heightDiff = getHeight(root.left) - getHeight(root.right);
if (Math.abs(heightDiff) > 1) {
return false;
}
return solution1(root.left) && solution1(root.right);
}
/**
* Improved
* */
public static boolean solution2(TreeNode root) {
return checkHeight(root) != Integer.MIN_VALUE;
}
private static int getHeight(TreeNode root) {
if (root == null) {
return -1;
}
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
private static int checkHeight(TreeNode root) {
if (root == null) {
return -1;
}
int leftHeight = checkHeight(root.left);
if (leftHeight == Integer.MIN_VALUE) return Integer.MIN_VALUE; // Propagate error up
int rightHeight = checkHeight(root.right);
if (rightHeight == Integer.MIN_VALUE) return Integer.MIN_VALUE; // Propagate error up
int heightDiff = leftHeight - rightHeight;
if (Math.abs(heightDiff) > 1) {
return Integer.MIN_VALUE; // Found error -> pass it back
}
return Math.max(leftHeight, rightHeight) + 1;
}
}