Bird
Raised Fist0
Data Structures Theoryknowledge~6 mins

Tree traversals (inorder, preorder, postorder) in Data Structures Theory - Full Explanation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Imagine you have a family tree or a map of connected places, and you want to visit every person or location in a certain order. Tree traversals are ways to visit all parts of a tree structure systematically, so you don't miss anything and follow a clear path.
Explanation
Preorder Traversal
In preorder traversal, you visit the current node first, then move to the left child, and finally the right child. This means you always process the parent before its children, which is useful for copying or saving the tree structure.
Preorder visits the parent node before its children.
Inorder Traversal
In inorder traversal, you first visit the left child, then the current node, and finally the right child. This order is especially useful for binary search trees because it visits nodes in ascending order.
Inorder visits nodes in a left-node-right sequence, producing sorted order in binary search trees.
Postorder Traversal
In postorder traversal, you visit the left child first, then the right child, and finally the current node. This order is helpful when you need to delete or free nodes because you process children before their parent.
Postorder visits children before their parent node.
Real World Analogy

Think of cleaning a house with rooms connected like a tree. Preorder is like checking the main room first, then cleaning connected rooms. Inorder is like cleaning the left side rooms first, then the main room, then the right side rooms. Postorder is like cleaning all connected rooms first before cleaning the main room.

Preorder Traversal → Checking the main room before its connected rooms
Inorder Traversal → Cleaning left rooms first, then main room, then right rooms
Postorder Traversal → Cleaning all connected rooms before the main room
Diagram
Diagram
       ┌─────┐
       │Root │
       └──┬──┘
          │
   ┌──────┴──────┐
┌──┴──┐        ┌─┴──┐
│Left │        │Right│
└─────┘        └─────┘

Traversal orders:
Preorder: Root → Left → Right
Inorder: Left → Root → Right
Postorder: Left → Right → Root
A simple binary tree showing the root with left and right children and the order of visiting nodes in each traversal.
Key Facts
Preorder TraversalVisits the current node before its children in the order: node, left child, right child.
Inorder TraversalVisits nodes in the order: left child, node, right child, producing sorted order in binary search trees.
Postorder TraversalVisits children before the node in the order: left child, right child, node.
Binary TreeA tree data structure where each node has at most two children called left and right.
TraversalA systematic way to visit all nodes in a tree.
Code Example
Data Structures Theory
class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def preorder(node):
    if node:
        print(node.value, end=' ')
        preorder(node.left)
        preorder(node.right)

def inorder(node):
    if node:
        inorder(node.left)
        print(node.value, end=' ')
        inorder(node.right)

def postorder(node):
    if node:
        postorder(node.left)
        postorder(node.right)
        print(node.value, end=' ')

# Example tree:
#       A
#      / \
#     B   C
#    / \
#   D   E

root = Node('A')
root.left = Node('B')
root.right = Node('C')
root.left.left = Node('D')
root.left.right = Node('E')

print('Preorder:')
preorder(root)
print('\nInorder:')
inorder(root)
print('\nPostorder:')
postorder(root)
OutputSuccess
Common Confusions
Thinking inorder traversal always visits nodes from top to bottom.
Thinking inorder traversal always visits nodes from top to bottom. Inorder visits nodes based on left-child, node, right-child order, which may not be top to bottom but left to right in the tree.
Believing preorder and postorder visit nodes in the same order.
Believing preorder and postorder visit nodes in the same order. Preorder visits the node before children, postorder visits children before the node, so their orders are different.
Summary
Tree traversals are methods to visit every node in a tree in a specific order.
Preorder visits the parent node before its children, inorder visits the left child, then parent, then right child, and postorder visits children before the parent.
Each traversal order is useful for different tasks like copying, sorting, or deleting tree nodes.

Practice

(1/5)
1. Which tree traversal visits the root node first, then the left subtree, and finally the right subtree?
easy
A. Preorder traversal
B. Inorder traversal
C. Postorder traversal
D. Level order traversal

Solution

  1. Step 1: Understand preorder traversal order

    Preorder traversal visits nodes in the order: root, left subtree, right subtree.
  2. Step 2: Compare with other traversal types

    Inorder visits left, root, right; postorder visits left, right, root; level order visits level by level.
  3. Final Answer:

    Preorder traversal -> Option A
  4. Quick Check:

    Root first = Preorder [OK]
Hint: Root first means preorder traversal [OK]
Common Mistakes:
  • Confusing inorder with preorder
  • Thinking postorder visits root first
  • Mixing level order with preorder
2. Which of the following is the correct order of nodes visited in an inorder traversal?
easy
A. Left, Right, Root
B. Root, Left, Right
C. Left, Root, Right
D. Right, Root, Left

Solution

  1. Step 1: Recall inorder traversal definition

    Inorder traversal visits nodes in the order: left subtree, root, right subtree.
  2. Step 2: Match with given options

    Left, Root, Right matches the left, root, right order exactly.
  3. Final Answer:

    Left, Root, Right -> Option C
  4. Quick Check:

    Inorder = Left, Root, Right [OK]
Hint: Inorder always visits left subtree before root [OK]
Common Mistakes:
  • Mixing preorder and inorder orders
  • Assuming root is visited first in inorder
  • Confusing right subtree position
3. Given the binary tree:
    A
   / \
  B   C
 /    
D   E   F

What is the postorder traversal sequence?
medium
A. D, B, E, F, C, A
B. A, B, D, E, C, F
C. B, D, E, A, C, F
D. D, E, B, F, C, A

Solution

  1. Step 1: Understand postorder traversal

    Postorder visits left subtree, right subtree, then root node.
  2. Step 2: Traverse given tree in postorder

    Left subtree of A: B subtree -> D, E, B; Right subtree of A: C subtree -> F, C; Finally root A.
  3. Final Answer:

    D, B, E, F, C, A -> Option A
  4. Quick Check:

    Postorder = Left, Right, Root [OK]
Hint: Postorder ends with root node [OK]
Common Mistakes:
  • Visiting root too early
  • Mixing preorder and postorder
  • Skipping right subtree nodes
4. A student wrote the preorder traversal of a tree as A, B, D, E, C, F but the inorder traversal as D, B, E, A, F, C. What is the error in the inorder traversal?
medium
A. The root node A is missing in inorder traversal
B. The nodes F and C are swapped in inorder traversal
C. The left subtree nodes are incorrectly ordered
D. No error, both traversals are consistent

Solution

  1. Step 1: Analyze preorder traversal

    Preorder: root A, left subtree B with children D and E, right subtree C with child F.
  2. Step 2: Check inorder traversal order

    Inorder should be left subtree (D, B, E), root A, right subtree (C, F). Given is D, B, E, A, F, C but F and C order is swapped.
  3. Final Answer:

    The nodes F and C are swapped in inorder traversal -> Option B
  4. Quick Check:

    Inorder right subtree order matters [OK]
Hint: Inorder right subtree nodes must be in correct order [OK]
Common Mistakes:
  • Ignoring subtree order in inorder
  • Assuming preorder and inorder can differ arbitrarily
  • Missing root node in traversal
5. You have a binary tree where the inorder traversal is D, B, E, A, C, F and the preorder traversal is A, B, D, E, C, F. Which of the following postorder traversals is correct?
hard
A. F, C, D, E, B, A
B. B, D, E, F, C, A
C. D, B, E, C, F, A
D. D, E, B, F, C, A

Solution

  1. Step 1: Use preorder to identify root and subtrees

    Preorder starts with A (root), then B subtree (D, E), then C subtree (F).
  2. Step 2: Use inorder to confirm subtree nodes

    Inorder left subtree: D, B, E; right subtree: C, F.
  3. Step 3: Construct postorder traversal

    Postorder visits left subtree (D, E, B), right subtree (F, C), then root (A).
  4. Final Answer:

    D, E, B, F, C, A -> Option D
  5. Quick Check:

    Postorder = Left, Right, Root [OK]
Hint: Postorder ends with root after left and right subtrees [OK]
Common Mistakes:
  • Mixing preorder and postorder sequences
  • Swapping left and right subtree orders
  • Placing root before subtrees in postorder