14/38
Part 3 of 7 · Your own stackLearn and do

Recursion was faster when it fit, and only your stack got through a chain

Both are worth knowing, so measure them.

Tree, 100,000 regionsRecursive all_sizessizes_with_stack
Random, each region under an earlier one, height 2337.5 ms54.3 ms
A chain, height 99,999RecursionError17.1 ms

Medians of seven runs, Python 3.12, on the machine these numbers come from. When recursion fits, it's the faster of the two here, and the stack version spends a second pass on the stored order. When it doesn't fit, nothing else matters.

Both are O(n)O(n) time. The memory differs in kind, and the two shapes that pull it apart are the chain and the star, one top holding everyone else:

Predict

A star: one top over 99,999 regions. At its peak, how many regions sit on your stack?

Pick an answer above to see the rest