# TAOCP 2.3.1 Traversing Binary Trees

> Study guide to TAOCP 2.3.1 Traversing Binary Trees. Binary trees, the three traversal orders (preorder, inorder, postorder), traversal with an explicit…

Updated October 20, 2026.

## Summary {#summary}

TAOCP 2.3.1 Traversing Binary Trees: Binary trees, the three traversal orders (preorder, inorder, postorder), traversal with an explicit stack instead of recursion, and threaded trees where spare null links point to inorder successors. Budget about 9 hours; do exercises 2.3-21, 2 and 4.

## Key facts {#key-facts}

- Week 55 of 145 on the cracked.guide path, in [TAOCP Vol 1: Fundamental Algorithms](https://cracked.guide/taocp/volume-1).
- Time budget: about 9 hours.
- 37 exercises in the section, rated 00 to HM41; the guide picks 8.
- Picked exercises with Knuth's triangle for recommended: 4, 17, 30 and 21.
- Before you start: 2.2.3 Linked Allocation and recursion.

## Week 55 goals {#objectives}

By the end of week 55 you can:

- Trace preorder, inorder and postorder traversals of a binary tree.
- Implement inorder traversal with an explicit stack instead of recursion.
- Explain how threads reuse null links to point to inorder neighbors.

## This week's steps {#steps}

- Read §2.3.1 Traversing Binary Trees: Read the opening pages of 2.3 (Trees) first: the definitions, the drawing conventions, Dewey notation, forests and Lists.
- Work the exercises (8 picked): Start with Ex 2.3-21, Ex 2, Ex 4; log what you solve below.
- Write down one thing you'd explain to someone: A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

## Exercises {#exercises}

This section has 37 exercises, rated 00 to HM41, and Knuth marks nine of them with the triangle for recommended. The 2.3 opener (Trees) has its own 22 exercises, rated 00 to HM32; one pick from it is listed as 2.3-21. In 2.3.1 itself, the traversal and threading exercises are the classics.

Worth your time:

- 2.3-21 (rated M22): From the 2.3 opener: count leaves from the numbers of nodes of each degree.
- 2 (rated 11): List a small tree's nodes in all three orders.
- 4 (rated 20, recommended): Look at a mirror-image traversal order and relate it to the standard ones.
- 7 (rated 22): Rebuild a binary tree from its preorder and inorder. The interview favorite, from the source.
- 13 (rated 24): Write a stack-based postorder traversal. The tricky one.
- 17 (rated 22, recommended): Find the preorder successor in a threaded tree.
- 30 (rated 22, recommended): Thread an unthreaded tree.
- 21 (rated 33, recommended): Traverse in inorder with no stack by temporarily rewiring links. The stretch.

## Why it matters {#why}

Tree traversal is everywhere: ASTs in your interpreters, DOM trees, file systems. Converting recursion to an explicit stack is a practical skill when recursion depth is a risk.

## Before you start {#before}

- 2.2.3 Linked Allocation
- Recursion

## How to approach it {#approach}

Read the opening pages of 2.3 (Trees) first: the definitions, the drawing conventions, Dewey notation, forests and Lists. The curriculum has no separate week for them and 2.3.1 assumes them. Then implement inorder traversal with an explicit stack and confirm it matches the recursive version on random trees. Build a threaded tree and walk it without any stack. Compare to the AST visitors from jlox.

## Common traps {#traps}

- Confusing the order in which you push left and right children.
- Threads and real links look identical without the tag bit. Keep the tag.

## Links {#links}

- [Knuth's TAOCP page (editions, errata, MIX/MMIX)](https://www-cs-faculty.stanford.edu/~knuth/taocp.html)
- [Vol 1 errata (plain text, from Knuth)](https://www-cs-faculty.stanford.edu/~knuth/err1.textxt)
- [Wikipedia: Tree traversal](https://en.wikipedia.org/wiki/Tree_traversal)
- [Wikipedia: Threaded binary tree](https://en.wikipedia.org/wiki/Threaded_binary_tree)

---

Source: https://cracked.guide/guide/knuth-w039-0
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
