Browse curriculum

Types of DFS (Tree Traversals)

Master Preorder, Inorder, and Postorder traversals: the building blocks of most Tree algorithms.

Medium
15 min
Solve on LeetCode

The Three Flavors of DFS

When traversing a Tree using Depth-First Search, the "depth-first" logic remains the same (go down before going sideways). However, the order in which we "process" (visit/print) the current node relative to its children defines the three main types:

  1. Preorder: Root → Left → Right
  2. Inorder: Left → Root → Right
  3. Postorder: Left → Right → Root

Problem: Standardize the Visit

Given a binary tree, return a list of values depending on the traversal order.

  • Input: Tree [1, null, 2, 3] (1 is root, 2 is right child, 3 is left child of 2)
  • Preorder Output: [1, 2, 3]
  • Inorder Output: [1, 3, 2]
  • Postorder Output: [3, 2, 1]

When to use each type?

Pattern Recognition:

  • Preorder: Creating a copy of the tree, Prefix expression evaluation, Serialization.
  • Inorder: Binary Search Tree (BST) validation (gives sorted order), Infix expression.
  • Postorder: Deleting a tree (delete children before parent), Postfix evaluation, Computing height/size (need child info first).

Intuition: The Tour Route

Imagine tracing the outline of the tree starting from the left of the root.

  • Preorder: Snap a photo the moment you arrive at a node for the first time.
  • Inorder: Snap a photo only after you've returned from the left child but before going right.
  • Postorder: Snap a photo only when you are leaving the node for the last time (finished both children).

Visualizing the Stack

Pick an order and run it. The walk is the same in all three — the outlined calls go down and come back up along the same path — and only the step where a node is appended moves. Each node shows the order it was appended in; switch orders on the same tree and watch the numbers change.

Dry Run Comparison

Tree: Root(1) -> Left(2), Right(3).

Step Preorder (Root-L-R) Inorder (L-Root-R) Postorder (L-R-Root)
1 Visit 1. Go Left(2). Go Left(2). Go Left(2).
2 Visit 2. Go Left(null). Go Left(null). Visit 2. Go Left(null). Go Right(null).
3 Back to 2. Go Right(null). Back to 2. Go Right. Back to 2. Visit 2. Return.
4 Back to 1. Go Right(3). Visit 1. Go Right(3). Back to 1. Go Right(3).
5 Visit 3. Children null. Left null. Visit 3. Left null. Right null. Visit 3.
6 Done. Done. Back to 1. Visit 1. Done.

The Code Templates

The three methods are identical except for one line: where result gets the node — before both calls, between them, or after both.

Types of DFS (Tree Traversals) Code
1function preorder(node, result) {
2 if (!node) return;
3 result.push(node.val);
4 preorder(node.left, result);
5 preorder(node.right, result);
6}
7
8function inorder(node, result) {
9 if (!node) return;
10 inorder(node.left, result);
11 result.push(node.val);
12 inorder(node.right, result);
13}
14
15function postorder(node, result) {
16 if (!node) return;
17 postorder(node.left, result);
18 postorder(node.right, result);
19 result.push(node.val);
20}

Iterative Approaches (Stack)

Using the call stack (recursion) is elegant, but sometimes we need to simulate it manually using an explicit stack.

  • Preorder: Push Root. While stack: Pop, process, push Right, push Left.
  • Inorder: Push all left nodes. Pop, process, move Right, repeat.
  • Postorder: Tricky (Use 2 stacks or reverse Preorder Root-Right-Left).

Edge Cases & Checks

Standard tree checks apply:

  • Empty Tree: Root is null. Return empty list.

  • Single Node: Left/Right are null. Recursion handles this (base case).

  • Skewed Tree: Effectively a Linked List. Recursion depth might hit limits (O(N)).

Summary

Recap:

  • Preorder: Root first.
  • Inorder: Root middle (Sorted for BST).
  • Postorder: Root last (Bottom-up processing).

The approach, step by step

  1. Walk the same path

    Return at an empty node, then recurse left and right.

  2. Choose when to append

    Append before both calls for preorder, between them for inorder, or after both for postorder.

  3. Collect the result

    The appended values, in order, are the traversal.

Frequently asked questions

What is the difference between preorder, inorder and postorder?

They follow the same path through the tree and differ only in when a node is processed: before its children (preorder), between them (inorder), or after both (postorder).


Why does inorder traversal of a BST come out sorted?

Everything in a node’s left subtree is smaller and everything in its right subtree is larger, so visiting left, then the node, then right lists the values in increasing order.


When is postorder the right choice?

When a node’s answer depends on its children’s answers, such as height, subtree sums or deleting a tree — the children must be finished first.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description