You can't use recursion to implement an iterator in C++ because your iterator needs to return from all processing before it can return the result.
Only languages like C# and Python, that have a concept of yield
can use recursion to create iterators.
Your iterator needs to maintain a stack of yet-to-be-visited nodes.
Of the top of my head, I think the algorithm is something like:
- Keep going down and to the left
- Every time you come across a right branch, add it to the stack
- If at any point you can't go left, pop the first branch off the stack and begin visiting that in the same way.