Binary Tree: Pre-order Traversal

Abhimanyu Singh
Data Structure and Algorithms
5 min readJul 5, 2021

--

Representation

We represent the node as:

Node =>
Value
Node Left
Node Right

Pre-order Traversal

The pre-order traversal is a kind of depth-first traversal. We perform the following steps:

  • Access the node
  • Recursively traverse the node’s left subtree in pre-order
  • Recursively traverse the node’s right subtree in pre-order

After traversing the left and the right subtrees of the root node, we consider the traversal complete.

Pre-order Traversal

Example

We will do the pre-order traversal on the following binary tree:

Binary Tree for Pre-order Traversal

The nodes in yellow are not yet visited, and the subtrees with dashed edges are also not visited yet. The pre-order traversal visits the nodes A, B, D, G, E, H, C, and F.

Pre-order Traversal

Let’s take a look at each visit separately. We start with the root node, i.e…

--

--

Abhimanyu Singh
Data Structure and Algorithms

Staff Software Engineer at HackerRank. Passionate about tech, career growth, and math. Exploring number theory and sharing insights on personal development.