Preorder Traversal

Preorder traversal is a depth-first search algorithm for a binary search tree that first traverses the root, then the left subree, and then the right subtree. Its primary use is to create a copy of the tree.


The preorder algorithm can be described as follows:

Function Preorder(tree)
  return root + Preorder(left-subtree) + Preorder(right-subtree)


For the following binary search tree:

Binary Search Tree Diagram

Preorder traversal provides the nodes in the following order: 4, 2, 1, 3, 6, 5, 7.


Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn More on Codecademy