0
Challenge · 41Trees

DFS: pre, in and post order

Treesused in ~100% of apps

Three famous names for one walk. The route never changes. All that changes is the moment you write the number down, and once you see that, all three stop needing to be memorised.

Core idea. Depth first search goes as deep as it can before backing up. At every node you do three things in some order: handle this node, go left, go right. Preorder handles the node first, inorder handles it in the middle, postorder handles it last. Same walk, three different moments.
Playground, this isn't a screenshot
the same seven nodes, visited in three different orders
nothing visited yet

Same walk every time. Only the moment you write the number down changes.

↑ play a little first, then dig in