The difference is simply when you process the current node relative to its children.

Suppose we have this tree:

      A
     / \
    B   C
   / \   \
  D   E   F

1. Pre-order DFS (Node → Left → Right)

You visit the current node first, then recursively visit its left subtree, followed by its right subtree.

A
├── B
│   ├── D
│   └── E
└── C
    └── F

Traversal order:

A → B → D → E → C → F

Pseudo-code:

def preorder(node):
    if not node:
        return

    visit(node)
    preorder(node.left)
    preorder(node.right)

Think of it as:

"Do something before exploring the children."

Typical uses:

  • Copying/cloning a tree
  • Serializing a tree
  • Creating prefix expressions
  • Any problem where the parent's value is needed before processing descendants

2. In-order DFS (Left → Node → Right)

You completely explore the left subtree, then visit the current node, then explore the right subtree.

Traversal:

D → B → E → A → C → F

Pseudo-code:

def inorder(node):
    if not node:
        return

    inorder(node.left)
    visit(node)
    inorder(node.right)

Think of it as:

"Process the node after finishing the left subtree but before the right."

Why is it famous?

For a Binary Search Tree, in-order traversal visits nodes in sorted order.

Example:

      5
     / \
    3   8
   / \ / \
  2 4 6 9

In-order:

2 3 4 5 6 8 9

This is why many BST interview questions use in-order traversal.


3. Post-order DFS (Left → Right → Node)

You process both children before processing the current node.

Traversal:

D → E → B → F → C → A

Pseudo-code:

def postorder(node):
    if not node:
        return

    postorder(node.left)
    postorder(node.right)
    visit(node)

Think of it as:

"Don't process me until both my children are finished."

This is perfect whenever the answer for a node depends on information from its children.

Examples:

  • Maximum depth
  • Balanced Binary Tree
  • Diameter of Binary Tree
  • Lowest common ancestor (many implementations)
  • Tree dynamic programming

Visual Comparison

For the same tree:

      A
     / \
    B   C
   / \   \
  D   E   F
TraversalOrder
Pre-orderA B D E C F
In-orderD B E A C F
Post-orderD E B F C A

Notice where A appears:

  • Pre-order: first
  • In-order: middle
  • Post-order: last

That's the entire difference.


How to Recognize Which One to Use

A simple interview rule is:

Use Pre-order if the parent decides what happens to the children.

Examples:

  • Print the tree
  • Clone a tree
  • Serialize a tree

Use In-order when working with a Binary Search Tree.

Examples:

  • Return sorted values
  • Validate a BST
  • Find the k-th smallest element

Use Post-order if the parent needs information from its children.

Examples:

  • Height
  • Diameter
  • Balanced tree
  • Maximum path sum
  • Count nodes in a subtree
  • Most tree dynamic programming problems

A useful way to remember

Imagine each node is a manager waiting for reports from two employees:

  • Pre-order: The manager gives instructions before the employees work.
  • In-order: The manager talks after the left employee finishes but before the right employee starts.
  • Post-order: The manager waits until both employees finish before making a decision.

This analogy explains why so many interview problems like Balanced Binary TreeDiameter of Binary Tree, and Maximum Depth naturally use post-order DFS: the parent can't determine its answer until both children have reported back.