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