CMD Guide
HomeDSATrees

BST Traversal Techniques

A BST traversal is a systematic walk over every node using the tree's own recursive left/right structure — the order in which you sequence the three actions (visit-left, visit-node, visit-right) determines whether you get sorted output, a rebuildable prefix, or a safe-to-delete postfix, so the traversal is really just a permutation of one recursive call.

Recognize the pattern

Brute force → optimal

Brute force: dump every node into an array via any order, then re-sort or re-filter to get what you need — e.g., collect all values and call Collections.sort() to get ascending order. Cost: O(n log n) time, ignoring the tree's structure entirely.

Optimal (recursive DFS): exploit the BST invariant directly — inorder visits left subtree (all smaller keys), then root, then right subtree (all larger keys), which is a sorted walk for free. O(n) time, O(h) space for the call stack.

Further optimal (Morris traversal): eliminate the O(h) stack by temporarily threading right pointers from each node's inorder predecessor back to itself, walking the thread, then removing it. O(n) time, O(1) space — at the cost of temporarily mutating the tree structure during the walk.

Complexity, derived

Recursive/iterative-stack DFS: the recurrence is T(n) = T(k) + T(n-k-1) + O(1) summed over all nodes, which telescopes to exactly n calls each doing O(1) work at the node itself → O(n) time. The stack depth at any moment equals the number of ancestors currently "open", which is bounded by the tree height h → O(h) space (O(log n) balanced, O(n) skewed, e.g. a tree built by inserting 1,2,3,...,n in order).

Morris traversal: each edge is traversed at most twice (once to create the thread, once to find and remove it while walking), so total pointer operations are bounded by 2n → still O(n) time, but no call stack or explicit stack is used → O(1) extra space (excluding the temporarily-modified tree itself, which is restored before returning).

Worked example

BST built by inserting 5, 3, 8, 1, 4, 7, 9 (shown in the diagram). Trace of the inorder recursive call stack:

CallActionOutput so far
inorder(5)recurse left → inorder(3)
inorder(3)recurse left → inorder(1)
inorder(1)no left; visit 1; no right1
inorder(3)visit 3; recurse right → inorder(4)1, 3
inorder(4)no children; visit 41, 3, 4
inorder(5)visit 5; recurse right → inorder(8)1, 3, 4, 5
inorder(8)recurse left → inorder(7); visit 71, 3, 4, 5, 7
inorder(8)visit 8; recurse right → inorder(9); visit 91, 3, 4, 5, 7, 8, 9

Result: 1, 3, 4, 5, 7, 8, 9 — sorted, as guaranteed by the BST invariant. Preorder on the same tree gives 5, 3, 1, 4, 8, 7, 9 (root-first, useful to rebuild the exact same tree shape by re-inserting in that order); postorder gives 1, 4, 3, 7, 9, 8, 5 (children fully processed before parent, so free(node) after visiting children never dereferences freed memory).

Java (all three, plus Morris inorder)

class Node {
  int val; Node left, right;
  Node(int v) { val = v; }
}

class Traversals {
  static void inorder(Node n, List<Integer> out) {
    if (n == null) return;
    inorder(n.left, out);
    out.add(n.val);
    inorder(n.right, out);
  }

  static void preorder(Node n, List<Integer> out) {
    if (n == null) return;
    out.add(n.val);
    preorder(n.left, out);
    preorder(n.right, out);
  }

  static void postorder(Node n, List<Integer> out) {
    if (n == null) return;
    postorder(n.left, out);
    postorder(n.right, out);
    out.add(n.val);
  }

  // Morris inorder: O(1) extra space, temporarily threads the tree.
  static List<Integer> morrisInorder(Node root) {
    List<Integer> out = new ArrayList<>();
    Node cur = root;
    while (cur != null) {
      if (cur.left == null) {
        out.add(cur.val);
        cur = cur.right;
      } else {
        Node pred = cur.left;
        while (pred.right != null && pred.right != cur) pred = pred.right;
        if (pred.right == null) {
          pred.right = cur;      // create thread
          cur = cur.left;
        } else {
          pred.right = null;     // remove thread, restore tree
          out.add(cur.val);
          cur = cur.right;
        }
      }
    }
    return out;
  }
}

Pitfalls

When to use / when not — trade-offs

ApproachTimeSpaceUse when
Recursive DFSO(n)O(h)Default choice; tree is reasonably balanced, code clarity matters.
Iterative w/ explicit stackO(n)O(h)Recursion depth risks stack overflow, or the interviewer explicitly bans recursion.
Morris traversalO(n)O(1)Memory-constrained environments; tolerates temporarily mutating the tree (not safe for concurrent readers).
Level-order (BFS, named alternative)O(n)O(w) widthNeed level-by-level results (e.g. tree width, serialization by level) — not a DFS order at all.

Do not reach for Morris traversal by default: it is trickier to get right (the thread-removal bug above) and mutates the tree mid-traversal, which is unacceptable if another thread might read the tree concurrently.

Takeaways

Recall: Why does an inorder traversal of a BST always produce ascending order, and what specifically breaks if you run it on an arbitrary (non-BST) binary tree?


Synthesized from standard BST traversal theory (Cormen et al., Introduction to Algorithms) and the Morris traversal technique (J.H. Morris, 1979, "Traversing Binary Trees Simply and Cheaply").

🤖 Don't fully get this? Learn it with Claude

Stuck on BST Traversal Techniques? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **BST Traversal Techniques** (DSA) and want to truly understand it. Explain BST Traversal Techniques from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **BST Traversal Techniques** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **BST Traversal Techniques** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **BST Traversal Techniques** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes