Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Preorder Tree Traversal in Binary Tree In C++
Preorder Tree Traversal
Preorder traversal is a depth first algorithm. There are three types of traversals in tree: Preorder, Inorder, Postorder. In this article, preorder tree traversal is performed using recursion.
More About Preorder Traversal
- We first print the value of the node,them move to the left subtree and finally to the right subtree.
- If we need to explore more about the node itself before inspecting its leaves, we use preorder traversal.
- Preorder traversal is used to find the prefix expression of an expression tree.
Steps To Find Preorder Traversal:
- Print the root node and traverse the left subtree.
- After printing 12, move to the left.
- 8 is printed and we move to the left subtree.
- Print 5 and move back,
- Since there is only 1 right child 13 is printed and we move to the right subtree.
- 54 and 18 are printed.
Algorithm To Find Preorder Traversal:
- If root is NULL, return NULL.
- Print the node.
- Visit left subtree.
- Visit right subtree.
15 12 8 5 13 54 18
Time Complexity To Find Preorder Traversal in Binary Tree