Search⌘ K
AI Features

Pre-Order Traversal

Explore the pre-order traversal technique for binary trees, where nodes are visited in root-left-right order. Understand the algorithm's recursive nature, see Python code examples, and analyze its linear time complexity to grasp how this traversal works in practice.

Introduction #

In this traversal, the elements are traversed in “root-left-right” order. We first visit the root/parent node, then the left child, and then the right child. Here is a high-level description of the algorithm for Pre-Order traversal, starting from the root node:

  1. Visit the current node, i.e., print the value stored at the node

  2. Call the preOrderPrint() function on the left sub-tree of the ‘current Node’.

  3. Call the preOrderPrint() function on the right sub-tree of the ‘current Node’.

Implementation in Python

...