← Tree Recursion

Pattern Recognition Drill

#38 — Preorder traversal

Easy Trees

The Problem

Return the preorder traversal of a binary tree as a list.

What approach would you use?

Think about it before scrolling down.

Key Signals

Tree Recursion

Append current, recurse left, recurse right. O(n) time.

Alt: Stack

Iterative: push root, pop and visit, push right then left (so left is processed first).

Common Trap

In iterative version, push right before left — stack is LIFO so left gets processed first.

← #37 #39 →