We can use binary search along with the $\mathsf{BPP}$ amplification theorem which states that a language $L$ is in the class $\mathsf{BPP}$ if for any polynomial $q(n)$, there exists a polynomial time algorithm $M(\cdot; \cdot)$ such that for all $x \in \{0, 1\}^n$,
- if $x \in L$, then $\underset{r}{\mathsf{Pr}}[M(x; r) = 1] \geq 1- \frac{1}{2^{q(n)}}$
- if $x \notin L$, then $\underset{r}{\mathsf{Pr}}[M(x; r) = 1] \leq \frac{1}{2^{q(n)}}$
Following a similar approach to the one in this post, let's assume that $L \in \mathsf{BPP}$ and let $A$ be a $\mathsf{BPP}$ algorithm for $L$ that satisfies the above probability with $q(n) = 2n$. We will design a probabilistic algorithm $B$ that inverts $f$ and has an expected polynomial running time in $n$ for invertible $y$. However, if $y$ does not have any preimage, the algorithm's runtime will be exponential in $n$.
The description of $B$ is as follows.
First, check whether $f(2^n−1)=y$ (i.e., whether the largest n-bit integer is a preimage). If true, return $2^n−1$.
Otherwise, check if $A(y,2^n−1)=1$. If this succeeds, use binary search to find $x$.
If the check fails or binary search doesn't succeed in finding a preimage, go over all $x \in \{0,1\}^n$ and checks and returns if $f(x) = y$.
Since step 2 uses the algorithm $A$, which is a $\mathsf{BPP}$ algorithm, there is a chance $A$ might give an incorrect answer. Nevertheless, in step 3, if $y$ has a preimage, $B$ will eventually return it.
Now, let's analyze the expected running time of $B$ for an input $y$ that has a preimage:
$$\text{Expected-Time}[B] = \mathsf{Pr}[A \text{ gives the right answers}]\cdot O(n) + \mathsf{Pr}[A \text{ gives at least one wrong answer}]\cdot 2^n$$
Next, we calculate the probability that step 2 will return a preimage. Since $A$ is invoked at most $2n$ times in step 2, $B$ succeeds in finding the preimage in step 2 if all invocations of $A$ give the correct answer. This occurs with high probability, $(1- \frac{1}{2^{2n}})^{2n}$, which we trivially bound by 1.
The probability that step 2 fails to find a preimage is $1-(1- \frac{1}{2^{2n}})^{2n}$. We bound the term $(1-(1- \frac{1}{2^{2n}})^{2n}) 2^n$ as follows.
$$\begin{align}
(1-(1- \frac{1}{2^{2n}})^{2n}) 2^n &= (1-(1 + \sum_{i=1}^{2n} {2n \choose i} \cdot (\frac{-1}{2^{2n}})^i)) 2^n\\
&= - \sum_{i=1}^{2n}\frac{-1^i \cdot {2n \choose i}}{2^{2ni - n}}\\
&\leq \sum_{i=1}^{2n}\frac{{2n \choose i}}{2^{2ni - n}}\\
&\leq \sum_{i=1}^{2n}\frac{(2n)^i}{2^{2ni - n}}\\
&\leq \sum_{i=1}^{2n}\frac{(2n)^i}{2^{ni}}\\
&\leq \sum_{i=1}^{2n}(\frac{2n}{2^{n}})^i \leq 2n
\end{align}$$
Using these bounds, we conclude:
$$\text{Expected-Time}[B] = 1\cdot O(n) + 2n \leq O(n)$$