What should I do if I have previously seen a problem on a homework assignment?
This is not a difficult situation, you worked on a problem and understood the solution, then having it set as an assignment - your grade is based on the effort you have put in - either during the course or before.
Benefit from it - other topics you may not be so lucky...
Whether it is an assignment or a final exam - this is NOT cheating - you put the work in and that's fine.
If the professor was meant to write "new" questions and did not, instead re-using old ones then that is their problem, and they may not see it as a problem ie back to "it's the work you put in".
Some students will do every problem they can find on a topic, others will only do two and say "that's enough" - apart from the students with "genius" level or a photographic memory then the grades tend to follow effort...
Do not dig out your notes from back then and copy them.
That's really all there is that could be an ethical issue. Solve the problem again, using your understanding of the material and memories of past studies - just like everyone else. You just got lucky that your past studies happened to include very detailed examination of this specific problem, and that you therefore know it thoroughly. Everyone sometimes gets lucky on an exam question that's about a topic that comes easily to them.
Edit to clarify: This is especially for the case where you have merely seen the answer in question in a textbook/lecture. We all agree that googling a problem and copying the solution from the textbook you found that way is wrong. Just because you happen to remember the name of the textbook doesn't make it right.
The goal of a graded exercise is to demonstrate that you can solve certain problems. If you still remember how to solve it, you don't need to look at your notes. If you have forgotten, you don't know how to solve it any more - so it would be cheating to pretend you do.
The other answers seem to suggest that you got lucky, which is something I disagree with entirely. Math classes are not a bunch of disparate courses or linearly organized. Last semester, I routinely gave exercises in a number theory class which duplicated material in an abstract algebra course, which some but not all of the students have had. The point was I wanted students to understand certain things from algebra for my course, but I can't assume everyone knows it as it is not a prerequiste.
At least in the US, students come into math classes (from lower-level to upper-level) with very different backgrounds and my goal for all of the students is to reach a certain level of proficiency and understanding of the topic, not to have each student learn at least X much material that is new to them. (Though if a student already knows almost everything in the class, they are probably in the wrong class.)
I am totally okay with exercise/exam problems being easier for people who have stronger backgrounds (or at least stronger in relevant areas). The only thing I am sometimes not happy with is if someone solves the exercises using results we haven't covered/assumed already in the course. If you're worried you might be doing this, or you want more challenging exercises, chat with the instructor.