private int height(TreeNode n) {
        if (n == null) return 0;
        int leftHeight = height(n.left);
        int rightHeight = height(n.right);
        if (leftHeight == -1 || rightHeight == -1) return -1;
        if (Math.abs(leftHeight - rightHeight) > 1) return -1;
        return Math.max(leftHeight, rightHeight) + 1;
    }