-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedBinaryTree.java
More file actions
40 lines (32 loc) · 1.05 KB
/
BalancedBinaryTree.java
File metadata and controls
40 lines (32 loc) · 1.05 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
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
int[] h = new int[1];
return isBalancedHelper(root, h);
}
//This is a O(n) solution, avoiding revisiting the same node
private boolean isBalancedHelper(TreeNode root, int[] h) {
boolean l; // Is left subtree balanced?
boolean r; // Is right subtree balanced?
int[] lh = new int[1]; // height of left subtree
int[] rh = new int[1]; // height of right subtree
if (null == root) {
h[0] = 0;
return true;
}
l = isBalancedHelper(root.left, lh);
r = isBalancedHelper(root.right, rh);
if (l == false || r == false) return false;
if ( (lh[0] - rh[0]) > 1 || (rh[0] - lh[0]) > 1 ) return false;
h[0] = Math.max(lh[0], rh[0]) + 1;
return true;
}
}