-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisBalanced.java
More file actions
36 lines (30 loc) · 998 Bytes
/
Copy pathisBalanced.java
File metadata and controls
36 lines (30 loc) · 998 Bytes
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
/*
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
*/
import java.lang.Math;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class isBalanced{
public boolean isBalanced(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) {return true;}
int hLeft = getHeight(root.left);
int rLeft = getHeight(root.right);
// balance conditions:
// 1. left and right height difference <= 1
// 2. left and right both are balanced
return Math.abs(hLeft - rLeft) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
// recursion, get the node height
public int getHeight(TreeNode v) {
int h = 0;
if (v == null) {return 0;}
return Math.max(getHeight(v.left), getHeight(v.right)) + 1;
}
public static void main(String[] args) {
}
}