-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck If Binary Tree Is Balanced.java
60 lines (50 loc) · 1.11 KB
/
Check If Binary Tree Is Balanced.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
/*
Check if a given binary tree is balanced.
A balanced binary tree is one in which the depths of every node’s left and right subtree differ by at most 1.
Example:
5
/ \
3 8
/ \ \
1 4 11
is balanced binary tree
What if the binary tree is null? Return true in this case.
time = O(nlogn), each level O(n), logn levels
space = O(log(n))
*/
/**
* public class TreeNode {
* public int key;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int key) {
* this.key = key;
* }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
// Write your solution here.
if (root == null) {
return true;
}
return getHeight(root) != -1;
}
public int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
int left = getHeight(root.left);
if (left == -1) {
return -1;
}
int right = getHeight(root.right);
if (right == -1) {
return -1;
}
if (Math.abs(left - right) > 1) {
return -1;
}
return Math.max(right, left) + 1;
}
}