Given a binary tree, check whether binary tree is 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.

For example, this binary tree is balance:
     5
   /  \
  3     3
 / \   / \
1   4 4   1

or
   5
 /  \
3    4

Example of not balanced binary tree :

     5
   /  
  3    
 / \  
1   4 

or
   5
 /  
3 
  \
   4
  /
 2
  public class BalancedBinaryTree {
    public boolean isBalanced(TreeNode root) {
    return depth(root) != -1;
  }

  public int depth(TreeNode root) {
    if (root == null)
      return 0;
    int lheight = depth(root.left);
    int rheight = depth(root.right);
    if (lheight == -1 || rheight == -1 || Math.abs(rheight - lheight) > 1)
      return -1;
    return Math.max(lheight, rheight) +1;
  }

  class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
      val = x;
    }
  }
}