-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedBinaryTree.java
More file actions
executable file
·40 lines (34 loc) · 1.31 KB
/
BalancedBinaryTree.java
File metadata and controls
executable file
·40 lines (34 loc) · 1.31 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
40
/**
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.
Hide Tags Tree Depth-first Search
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.concurrent.atomic.AtomicInteger;
public class BalancedBinaryTree {
public boolean isBalanced(TreeNode root) {
return isBalanced(root, new AtomicInteger());
}
private boolean isBalanced(TreeNode node, AtomicInteger height) {
if (node == null) {
height.set(0);
return true;
}
AtomicInteger leftHeight = new AtomicInteger();
boolean leftBal = isBalanced(node.left, leftHeight);
AtomicInteger rightHeight = new AtomicInteger();
boolean rightBal = isBalanced(node.right, rightHeight);
if (!leftBal || !rightBal) return false;
if (Math.abs(leftHeight.intValue() - rightHeight.intValue()) > 1) return false;
height.set((leftHeight.intValue() > rightHeight.intValue() ? leftHeight.intValue() : rightHeight.intValue()) + 1);
return true;
}
}