-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalancedBinary
24 lines (24 loc) · 1.03 KB
/
balancedBinary
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public boolean isBalanced(TreeNode root) {
// If the tree is empty, we can say it’s balanced...
if (root == null) return true;
// Height Function will return -1, when it’s an unbalanced tree...
if (Height(root) == -1) return false;
return true;
}
// Create a function to return the “height” of a current subtree using recursion...
public int Height(TreeNode root) {
// Base case...
if (root == null) return 0;
// Height of left subtree...
int leftHeight = Height(root.left);
// Height of height subtree...
int rightHight = Height(root.right);
// In case of left subtree or right subtree unbalanced, return -1...
if (leftHeight == -1 || rightHight == -1) return -1;
// If their heights differ by more than ‘1’, return -1...
if (Math.abs(leftHeight - rightHight) > 1) return -1;
// Otherwise, return the height of this subtree as max(leftHeight, rightHight) + 1...
return Math.max(leftHeight, rightHight) + 1;
}
}