1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public boolean isBalanced(TreeNode root) { if (root == null) { return true; } return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right); }
int height(TreeNode node) { if (node == null) { return 0; } else { return Math.max(height(node.left), height(node.right)) + 1; } } }
|