Chapter 11 - Trees
Most tree problems are solved in two ways: recursion that goes deep first, or a queue that goes level by level.
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::cloneto share. std::collections::VecDequeis 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
- p189 - Introduction: Introduction to the Trees pattern for coding interviews, in Rust: the key idea, when to use it, and the problems of the chapter.
- p193 - Invert Binary Tree: Invert Binary Tree (Trees), solved in Rust. Invert a binary tree and returns its root.
- p197 - Balanced Binary Tree Validation: Balanced Binary Tree Validation (Trees), solved in Rust. Determine if a binary tree is height-balanced.
- p200 - Rightmost Nodes of a Binary Tree: Rightmost Nodes of a Binary Tree (Trees), solved in Rust. Return an array containing the values of the rightmost nodes at each level of a binary tree.
- p204 - Widest Binary Tree Level: Widest Binary Tree Level (Trees) solved in Rust: return the width of the widest level, from the leftmost to the rightmost non-null node.
- p207 - Binary Search Tree Validation: Binary Search Tree Validation (Trees) solved in Rust: verify that a binary tree is a valid BST, with lower values on the left and higher on the right.
- p212 - Lowest Common Ancestor: Lowest Common Ancestor (Trees), solved in Rust. Return the lowest common ancestor (LCA) of 2 nodes. A node can be considered as ancestor of itself.
- p216 - Build Binary Tree from Preorder and Inorder Traversals: Build Binary Tree from Preorder and Inorder Traversals (Trees) solved in Rust: rebuild a binary tree from the values of both traversals.
- p221 - Maximum Sum of a Continuous Path in a Binary Tree: Maximum Sum of a Continuous Path in a Binary Tree (Trees) solved in Rust: the largest sum of a path with at least one node.
- bonus087 - Binary Tree Symetry: Binary Tree Symetry (Trees), solved in Rust. Determine if a binary tree is vertically symmetric. That is, the left subtree of the root node is a.
- bonus091 - Binary Tree Columns: Binary Tree Columns (Trees), solved in Rust. Given the root of a binary tree. Return a list of arrays. Where each array represents a vertical column of the…
- bonus096 - Kth Smallest Number in a Binary Search Tree: Kth Smallest Number in a Binary Search Tree (Trees) solved in Rust: given the root of a BST and an integer k, find the kth smallest node.
- bonus101 - Serialize and Deserialize a Binary Tree: Serialize and Deserialize a Binary Tree (Trees), solved in Rust. Write a function to serialize a binary tree into a string. Write another function to…
Other chapters
All the chapters, with every problem, are listed on the series page.