Chapter 11 - Trees

Most tree problems are solved in two ways: recursion that goes deep first, or a queue that goes level by level.

Coding Interview Patterns book cover with the Rust logo and a CHAP 11 label, the chapter on trees

This page is the entry point of chapter 11 of my Rust solutions to the book Coding Interview Patterns. It sums up the pattern in a few lines, then lists the problems of the chapter. As explained on the series page, the detailed explanations of the algorithms are in the book.

The idea

With a depth-first search, we solve the problem for the left and right subtrees, then combine the two answers at the current node: height, balance, maximum path. With a breadth-first search, a queue visits the tree level by level: rightmost nodes, widest level. In a binary search tree, an in-order traversal visits the values in sorted order.

The chapter also has an introduction page with my notes on the key ideas and a checklist.

When to reach for it

  • Height, balance, symmetry, paths: think depth-first recursion.
  • Anything “per level”: think breadth-first with a queue.
  • A binary search tree: use its ordering to skip whole subtrees.

In Rust

  • Nodes are often Option<Rc<RefCell<TreeNode>>>, LeetCode style: borrow() to read, borrow_mut() to modify, Rc::clone to share.
  • std::collections::VecDeque is the queue of the breadth-first search.
  • A recursive helper that returns a tuple (for example height and balanced) avoids visiting the same subtree twice.

Problems in this chapter

Other chapters

All the chapters, with every problem, are listed on the series page.


Back to top

Published on: Dec 1 2026 at 10:00 AM | Last updated: Dec 1 2026 at 10:32 AM

Copyright © 1964-2026 - 40tude