-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalancedBinaryTree.java
39 lines (35 loc) · 1.22 KB
/
BalancedBinaryTree.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
/**
* 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 differs by more than 1.
*/
public class BalancedBinaryTree {
public static void main (String[] args){
System.out.println("Is tree balanced : " + isBalanced(getTreeRootNode()));
}
public static boolean isBalanced(TreeNode root) {
return maxDepth(root) != -1;
}
private static int maxDepth(TreeNode root) {
if(root == null) return 0;
int left = maxDepth(root.left);
if(left == -1) return -1;
int right = maxDepth(root.right);
if(right == -1) return -1;
return (Math.abs(left - right) <= 1) ? Math.max(left, right) + 1 : -1;
}
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public static TreeNode getTreeRootNode(){
TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.left.left = new TreeNode(3);
root.left.left.left = new TreeNode(1);
root.right = new TreeNode(15);
return root;
}
}