Skip to content
StackDecode
TreesAnimated

Preorder Traversal

Node, left, right. Visits parents before children.

Time

O(n)

Extra space

O(h)

Use it for

Copying or serialising a tree.

How it works

  1. Visit the node first.
  2. Then traverse its left subtree.
  3. Then traverse its right subtree.
  4. Parents always come before their children, so it can be used to copy or save a tree.

Watch it run

Loading animation…