10/36
Part 2 of 8 · RecursionLearn and do

The stack changed the order, and a flatten must keep it

The stack version of total added the items of [1, [2, 3], [4, [5]]] in the order 1, 4, 5, 2, 3. The recursion reads them 1, 2, 3, 4, 5. The stack version finishes the list in hand before it opens the lists it found inside, and the list it pushed last comes off first. A sum doesn't care. A flatten does.

Flatten Nested List Iterator is on the compiled LeetCode list for OpenAI (1 source). An iterator hands out one item per call. A recursive generator can do that too, but it hits the same recursion limit. Flattening everything up front does all the work before the first item is asked for. So the iterator keeps a stack itself, and it must hand the items out in reading order.

In the editor, write flatten(nested) with a stack you keep yourself, no recursion. It returns the items in reading order, 1, 2, 3, 4, 5, not the order the stack version of total added them.