Does $\langle\psi|\hat{A}|\psi\rangle = \langle\psi|\hat{B}|\psi\rangle $ for all $|\psi\rangle$ imply that $\hat{A} = \hat{B}$? I've to solve this simple problem:

Let $\hat{A}$ and $\hat{B}$ be two Hermitian operators such that:
$$ \langle\psi|\hat{A}|\psi\rangle  = \langle\psi|\hat{B}|\psi\rangle \qquad \forall |\psi\rangle  $$
Prove that $$\hat{A} = \hat{B}.$$
Hint: expand $|\psi\rangle$ in some suitable basis.

I've naively tried:
$$ \langle\psi|\hat{A}-\hat{B}|\psi\rangle  = 0 \qquad \forall |\psi\rangle  $$
Therefore $\hat{A}-\hat{B}=0$ exactly.
But this seems to work ever with non-hermitian operators. Am I wrong?
 A: Yes, OP is right: It holds for not necessarily selfadjoint operators$^1$.
Sketched proof:

*

*By subtracting $\hat{B}$ from both operators (and renaming), we may assume w.l.o.g. that $\hat{B}=\hat{0}$.


*Rewrite $\hat{A}=\hat{A}_1+i\hat{A}_2$, where $\hat{A}_1=\frac{\hat{A}+\hat{A}^{\dagger}}{2}$ and $\hat{A}_2=\frac{\hat{A}-\hat{A}^{\dagger}}{2i}$ are selfadjoint operators.


*Use the polarization trick to show that
$$\begin{align}
\forall | \phi\rangle, | \psi\rangle\in {\cal H}:~~
0~=~&\langle \phi+\psi | \hat{A} |\phi+ \psi\rangle\cr
&-\langle \phi | \hat{A} | \phi\rangle 
-\langle \psi | \hat{A} | \psi\rangle \cr
~=~&\langle \phi | \hat{A} | \psi\rangle +\langle \psi | \hat{A} | \phi\rangle\cr
~=~&2{\rm Re}\langle \phi | \hat{A}_1 | \psi\rangle\cr
&+2i{\rm Re}\langle \phi | \hat{A}_2 | \psi\rangle.\end{align}$$


*Deduce that $$\forall | \phi\rangle, | \psi\rangle\in {\cal H}:~~
{\rm Re}\langle \phi | \hat{A}_1 | \psi\rangle
~=~0~=~{\rm Re}\langle \phi | \hat{A}_2 | \psi\rangle.$$


*Deduce that $$\forall | \phi\rangle, | \psi\rangle\in {\cal H}:~~
\langle \phi | \hat{A}_1 | \psi\rangle
~=~0~=~\langle \phi | \hat{A}_2 | \psi\rangle.$$


*Conclude that $\hat{A}=\hat{0}$.$\Box$

$^{1}$We will ignore subtleties with unbounded operators, domains, selfadjoint extensions, etc., in this answer.
