/**
* 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) {
if (root == null)
return true;
int left_height = max(root.left);
int right_height = max(root.right);
return (Math.abs(left_height-right_height)<=1)&&isBalanced(root.left)&&isBalanced(root.right);
}
private int max(TreeNode root){
if (root == null ) return 0;
int left_height = max(root.left);
int right_height = max(root.right);
return (left_height>right_height)?left_height+1:right_height+1;
}
}