-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalanced Binary Tree.java
59 lines (51 loc) · 1.32 KB
/
Balanced Binary Tree.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
/*
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.
Example
Given binary tree A = {3,9,20,#,#,15,7}, B = {3,#,20,15,7}
A) 3 B) 3
/ \ \
9 20 20
/ \ / \
15 7 15 7
time = O(nlogn) -> O(n)
space = O(n)
*/
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
// write your code here
if (root == null) {
return true;
}
return getHeight(root) != -1;
}
private 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(right - left) > 1) {
return -1;
}
return Math.max(right, left) + 1;
}
}