-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBalanced Binary Tree.java
More file actions
39 lines (36 loc) · 1.11 KB
/
Balanced Binary Tree.java
File metadata and controls
39 lines (36 loc) · 1.11 KB
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
/*
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.
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (root == null) {
return true;
}
int diff = getHeight(root.left) - getHeight(root.right);
if (Math.abs(diff) > 1) {
return false;
} else {
return isBalanced(root.left) && isBalanced(root.right);
}
}
public int getHeight(TreeNode node) {
if (node == null) {
return 0;
}
int left = getHeight(node.left) + 1;
int right = getHeight(node.right) + 1;
return left > right? left : right;
}
}