Decide Whether A Binary Tree Is Balanced
Java:
class Node {
public Node left, right;
private static int height(Node n) {
if (n == null) return 0;
return 1 + Math.max(height(n.left), height(n.right));
}
public static boolean isBalanced(Node n) {
if (n == null) return true;
if (Math.abs(height(n.left) - height(n.right)) > 1) return false;
return isBalanced(n.left) && isBalanced(n.right);
}
}