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