-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedBinaryTree.java
More file actions
37 lines (29 loc) · 897 Bytes
/
BalancedBinaryTree.java
File metadata and controls
37 lines (29 loc) · 897 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
37
/*
Given a binary tree, determine if it is height-balanced.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: true
*/
class Solution {
public boolean isBalanced(TreeNode root) {
// tree is null
if(root == null) return true;
boolean isAVL[] = new boolean[1];
isAVL[0] = true;
int temp = helper(root, isAVL);
return isAVL[0];
}
private int helper(TreeNode root, boolean[] flag){
// leaf node
if(root.left == null && root.right == null)
return 1;
// internal node
int lst_h, rst_h;
lst_h = (root.left != null) ? helper(root.left, flag) : 0;
rst_h = (root.right != null) ? helper(root.right, flag) : 0;
int val = lst_h - rst_h;
if(val < -1 || val > 1)
flag[0] = false;
return 1 + Math.max(lst_h, rst_h);
}
}