-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathCheckBalancedTree.java
65 lines (59 loc) · 1.52 KB
/
CheckBalancedTree.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
61
62
63
64
65
package chapter04TreesAndGraphs;
/**
*
* Problem: 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 CheckBalancedTree {
/**
* method 1: O(NlogN)
*/
private int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
if (Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) {
return false;
} else {
return isBalanced(root.left) && isBalanced(root.right);
}
}
/**
* method 2: O(N)
*/
class ResultType {
boolean isBalanced;
int maxDepth;
public ResultType(boolean isBalanced, int maxDepth) {
this.isBalanced = isBalanced;
this.maxDepth = maxDepth;
}
}
public boolean isBalanced2(TreeNode root) {
return helper(root).isBalanced;
}
private ResultType helper(TreeNode root) {
if (root == null) {
return new ResultType(true, 0);
}
ResultType left = helper(root.left);
ResultType right = helper(root.right);
// subtree is not balanced
if (!left.isBalanced || !right.isBalanced) {
return new ResultType(false, -1);
}
// root is not balanced
if (Math.abs(left.maxDepth - right.maxDepth) > 1) {
return new ResultType(false, -1);
}
return new ResultType(true, Math.max(left.maxDepth, right.maxDepth) + 1);
}
}