A stack that remembers your place
transcript
A reader wants the ids in order. But only one at a time. Do not copy the whole tree. Keep a stack instead.
At the start, push the left edge. Eight, three, then one. One is on top. The first call takes one. Nothing is on its right. The stack holds eight and three.
The next call takes three. Then push the left edge of its right side. That is six, and four below it. The stack holds eight, six, four. Four is next.
Each id is pushed once and taken once. So one call costs one step on average. The stack holds one path, not the tree. A copy of the tree would cost far more memory.
Take the top. Push the left edge of its right side. A mistake: push only the right child, and the order is wrong. Each id is read at most once. Full breakdown in the lecture.