Back to All Algorithms
Pre-order Traversal
Category: Tree / Graph | Time: O(n) | Space: O(h)
Visualization
Visual representation of the data structure
2
7
5
12
18
15
10
About Pre-order Traversal

A tree traversal method that visits nodes in the order: root, left child, right child. It's used to create a copy of the tree.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
Variable State
{
  "status": "starting traversal"
}
Pre-order Traversal: How It Works
Last updated October 28, 2026

Overview

Pre-order traversal visits the current node first, then its left subtree, then its right subtree. Because each parent is visited before any of its descendants, the output describes the tree from the top down.

That top-down order is exactly what you need to copy or serialise a tree: if you write nodes in pre-order (including markers for null children), you can rebuild the identical tree by reading them back in the same order. File-system listings and expression trees printed in prefix (Polish) notation follow the same pattern.

On the default tree (root 10, children 5 and 15, leaves 2, 7, 12, 18), pre-order visits 10, 5, 2, 7, 15, 12, 18. Notice that the root always comes first and that the whole left branch is finished before 15 is visited.

Step by step

  1. If the current node is null, return.
  2. Visit the current node.
  3. Recursively traverse the left subtree.
  4. Recursively traverse the right subtree.

Complexity

CaseCostWhy
TimeO(n)Each node is visited once.
Space (balanced tree)O(log n)Recursion depth equals tree height.
Space (skewed tree)O(n)Height can be n.

When to use it

  • Cloning or serialising a tree so it can be rebuilt later.
  • Printing hierarchical data such as folders or an organisation chart with indentation.
  • Converting an expression tree to prefix notation.

Common mistakes

  • In an iterative version with a stack, push the right child before the left so the left is processed first.
  • Serialising without null markers loses information — different trees can produce the same sequence.
  • Deep recursion on skewed trees can overflow the stack.
Worked Example: Pre-order Traversal Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Pre-order Traversal: follow each row, predict the next one, and compare. The run below takes 60 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.

Input
Binary tree (see the input box above)
Result
final_path = [10, 5, 2, 7, 15, 12, 18]
StepLineWhat happens
11status = starting traversal
35current_node = 10, action = visiting node
58from_node = 10, action = traversing left
75current_node = 5, action = visiting node
98from_node = 5, action = traversing left
115current_node = 2, action = visiting node
138from_node = 2, action = traversing left
153node = is null, action = return
169from_node = 2, action = traversing right
183node = is null, action = return
1911finished_subtree_at = 2, action = return
209from_node = 5, action = traversing right
225current_node = 7, action = visiting node
248from_node = 7, action = traversing left
263node = is null, action = return
279from_node = 7, action = traversing right
293node = is null, action = return
3011finished_subtree_at = 7, action = return
3111finished_subtree_at = 5, action = return
329from_node = 10, action = traversing right
345current_node = 15, action = visiting node
368from_node = 15, action = traversing left
385current_node = 12, action = visiting node
408from_node = 12, action = traversing left
423node = is null, action = return
439from_node = 12, action = traversing right
453node = is null, action = return
4611finished_subtree_at = 12, action = return
479from_node = 15, action = traversing right
495current_node = 18, action = visiting node
518from_node = 18, action = traversing left
533node = is null, action = return
549from_node = 18, action = traversing right
563node = is null, action = return
5711finished_subtree_at = 18, action = return
5811finished_subtree_at = 15, action = return
5911finished_subtree_at = 10, action = return
6014final_path = [10, 5, 2, 7, 15, 12, 18]
Frequently Asked Questions
About Pre-order Traversal

What is Pre-order Traversal?

Pre-order traversal is a depth-first traversal method for trees. The order of visiting nodes is: Root, Left subtree, Right subtree. It's often used to create a copy of a tree or to get a prefix expression of an expression tree.

How does Pre-order traversal work?

Starting from the root, the algorithm first visits the current node (e.g., prints its value or adds it to a list). Then, it recursively calls itself on the left child, and after the entire left subtree is traversed, it recursively calls itself on the right child.

What is a key use case for Pre-order traversal?

It is used to create a copy of a tree. By inserting nodes in pre-order sequence, you can replicate the exact structure of the original tree. It is also used to get a prefix expression from an expression tree (e.g., `+ A B`).

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.