VOOZH about

URL: https://www.geeksforgeeks.org/dsa/preorder-vs-inorder-vs-postorder/

⇱ Preorder vs Inorder vs Postorder - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Preorder vs Inorder vs Postorder

Last Updated : 23 Jul, 2025

In Preorder Traversal, the root node is visited first, followed by the left and right subtrees. Inorder Traversal starts with the left subtree, visits the root, and then the right subtree, often used in binary search trees. Postorder Traversal begins with the left subtree, moves to the right subtree, and visits the root last, useful for deleting nodes. In this article, we will discuss the Tree Traversal Techniques used in data structure and algorithms with their differences.

Preorder traversal is defined as a type of tree traversal that follows the Root-Left-Right policy where:

  • The root node of the subtree is visited first.
  • Then the left subtree is traversed.
  • At last, the right subtree is traversed.


Inorder traversal is defined as a type of tree traversal technique that follows the Left-Root-Right pattern, such that:

  • The left subtree is traversed first.
  • Then the root node for that subtree is traversed.
  • Finally, the right subtree is traversed.


Postorder traversal is defined as a type of tree traversal that follows the Left-Right-Root policy such that for each node:

  • The left subtree is traversed first.
  • Then the right subtree is traversed.
  • Finally, the root node of the subtree is traversed.

Below is the Implementation of the above idea:


Output
Pre-order traversal: 1 2 4 5 3 6 
In-order traversal: 4 2 5 1 3 6 
Post-order traversal: 4 5 2 6 3 1 

Attribute

Pre-order

In-order

Post-order

Visit Order

Root, Left Subtree, Right Subtree

Left Subtree, Root, Right Subtree

Left Subtree, Right Subtree, Root

Sorted Order (BST)

No

Yes

Yes

Expression Tree

Prefix (Polish) Notation

Infix (Standard) Notation

Postfix (Reverse Polish) Notation

Evaluation Order

Operators come before operands

Operators come between operands

Operators come after operands

Memory Usage

No additional memory for tracking traversal, stack space for recursion/iteration

No additional memory for tracking traversal, stack space for recursion/iteration

No additional memory for tracking traversal, stack space for recursion/iteration

Left-Right Symmetry

Not symmetric

Symmetric

Not symmetric

Application

Constructing expression trees, cloning/copying trees, prefix notation

In-order traversal of BST for sorted order, expression evaluation

Deleting nodes in binary trees, post-fix notation, expression evaluation

Performance

Better for copying/cloning trees, prefix notation

Suitable for sorting BST elements, expression evaluation

Suitable for deleting nodes, post-fix notation, expression evaluation

Tree Modification

Easy to copy/cloning, preserving original structure

Not ideal for modification

Difficult for modification, requires reversing

Comment