Data Structures · Binary Trees and Traversals
Inorder, Preorder and Postorder Traversal Definitions
Overview
Inorder, preorder and postorder are the three most important traversal methods for binary trees. Each is defined by when the root of a subtree is visited relative to its left and right subtrees, and the rule applies recursively at every node.
Key concepts
- Inorder traversal — finish the complete left subtree, then visit (print) the root, then finish the right subtree. Order: Left → Root → Right.
- Preorder traversal — visit the root first, then the left subtree, then the right subtree. Order: Root → Left → Right.
- Postorder traversal — finish the left subtree, then the right subtree, and visit the root at the end. Order: Left → Right → Root.
- Meaning of the prefixes — "in", "pre" and "post" refer only to the order in which the root node is visited: in the middle (inorder), before everything (preorder), after everything (postorder).
- Fixed child order — in all three methods the left child is always visited before the right child.
- Recursive view — whenever a node is presented, it is treated as the root of its own subtree, so the traversal rule applies at every node.
- Scope — these traversals are defined for binary trees only. Extensions have been proposed for general trees (nodes with 3, 4 or at most 5 children), but the standard forms are for binary trees.

How it works
- Take the current node as the root of its own subtree.
- Apply the order named by the traversal:
- Inorder: complete the left subtree, visit the root, complete the right subtree.
- Preorder: visit the root, complete the left subtree, complete the right subtree.
- Postorder: complete the left subtree, complete the right subtree, visit the root.
- Apply the same rule recursively at every node reached.
- Direct evaluation is easy for a small, simple tree; for a very big tree the direct method becomes complex.
Formulas
- Inorder:
- Preorder:
- Postorder:
Worked examples
Tree: root A, left child B, right child C.
- Inorder: visit left child B first, then root A, then right child C → B, A, C
- Preorder: visit root A first, then left B, then right C → A, B, C
- Postorder: visit left B first, then right C, then root A → B, C, A
Comparison
| Traversal | Order of visiting | Position of root | Result for tree A (left B, right C) |
|---|---|---|---|
| Inorder | Left → Root → Right | Middle | B, A, C |
| Preorder | Root → Left → Right | First | A, B, C |
| Postorder | Left → Right → Root | Last | B, C, A |
Revision summary
- The prefixes in / pre / post describe only where the root is visited; left-before-right is fixed in all three.
- Inorder = Left, Root, Right; Preorder = Root, Left, Right; Postorder = Left, Right, Root.
- Each node is the root of its own subtree, so the rule applies recursively at every node.
- For root A with left child B and right child C: inorder = B A C, preorder = A B C, postorder = B C A.
- These traversals are defined for binary trees; extensions exist for trees with more children (up to about 5).
- Direct evaluation is simple for small trees but becomes complex for very big trees.
Previous-year exam insights
- A binary tree is uniquely determined only by the pair (inorder + preorder) or (inorder + postorder); preorder + postorder alone is not sufficient in general (it is sufficient only for a full/proper binary tree where every node has 0 or 2 children).
Asked inGATE 2024GATE 2004 - Reconstruction method: the first element of preorder (or the last element of postorder) is the root; its position in the inorder sequence splits the remaining elements into the left-subtree and right-subtree sets, and the same split is applied recursively.
Asked inGATE 2018GATE 2007 - For a BST, the inorder traversal is always the sorted (increasing) order of the keys, so a BST can be reconstructed from its preorder or postorder alone; in preorder the root comes first and all keys smaller than it form the left subtree, all larger keys the right subtree.
Asked inGATE 2020GATE 2017GATE 2013GATE 2005GATE 2008 - A BST can be built from its postorder in time (e.g. by scanning the postorder from the end with a stack, or by using key-range bounds), and from its preorder in time as well; a general binary tree from inorder plus preorder/postorder takes time with hashing or with search.
Asked inGATE 2008 - Traversal sequences of the same tree are consistent: the first element of preorder and the last element of postorder are the same node (the root), and the set of nodes in all traversals is identical; these checks quickly eliminate wrong options.
Asked inGATE 2008 - The height of a tree (longest root-to-leaf path in edges) can be obtained from the reconstructed tree, or recursively as 1 + max(height of left, height of right) with height of an empty tree = -1 and of a leaf = 0.
Asked inGATE 2018