public class Solution {
public boolean isBalanced(TreeNode root) {
// do an inorder traverse
if (root == null) return true;
if (Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) return false;
if (!isBalanced(root.left) || !isBalanced(root.right)) return false;
return true;
}
// determine the height of a given tree t
public static int getHeight(TreeNode t) {
if (t == null) return 0;
return 1 + Math.max(getHeight(t.left), getHeight(t.right));
}
}