Topic · 4 patterns
Trees patterns
Tree problems are recursion with one question: what does each node return to its parent? BFS walks a tree level by level, DFS carries values down a path and answers back up, BST logic uses the ordering to discard half the tree, and construction and serialisation rebuild a tree from its traversals.
The patterns
Tree BFS (Level Order)
O(n)
Process the tree level by level with a queue, recording the size of each level before draining it.
Spot it: Anything per level: level order, averages, right-side view, zigzag, level width, or minimum depth / nearest leaf.
Tree DFS (Paths & Recursion)
O(n)
Recurse down carrying state (running sum / depth); combine children's results on the way back up.
Spot it: Root-to-leaf paths, path sums, LCA, or a value computed from children: height, diameter, balance, max path sum.
BST Logic
O(h)
Use the ordering invariant: go left for smaller, right for larger; an inorder traversal yields sorted values.
Spot it: The tree is a binary search tree: kth smallest, successor, range sum, closest value, validate or recover ordering, LCA by value.
Learn first: Tree DFS (Paths & Recursion), Binary Search
Tree Construction & Serialization
O(n)
Preorder/postorder gives the root; inorder splits left vs right subtree. Recurse on the partitions.
Spot it: Rebuild a tree from two traversals (preorder+inorder), serialize/deserialize to a string, or flatten/convert its structure.
Learn first: Tree DFS (Paths & Recursion)
Which one? Match the signal
| If the problem says | Reach for |
|---|---|
| "Level order", level-by-level, right-side view, zigzag, or minimum depth | Tree BFS |
| Root-to-leaf paths, path sums, or a recursive subtree property (height, diameter) | Tree DFS |
| It says binary search tree; inorder is sorted; "kth smallest" / "validate" | BST Logic |
| Rebuild a tree from traversals, or serialize / deserialize it | Tree Construction |
Easy to confuse
Minimum Height Trees looks like Tree DFS (Paths & Recursion), but it’s Topological Sort
Trying every root is O(n2); peeling leaves (Kahn on degree 1) finds centres.
Used together
Tree DFS (Paths & Recursion) + Prefix Sum + Hash Map / Hash Set Lookup
When: count downward paths with a given sum. Downward paths starting anywhere are pairs of prefixes on the current root path.
Tree DFS (Paths & Recursion) + Graph BFS / DFS
When: nodes at distance k from a target node; infection time from a node. Distances in a tree can go upward, which child pointers cannot express.
Binary Lifting & LCA + Tree DFS (Paths & Recursion)
When: distance between two nodes, k-th ancestor, many queries. Many LCA/distance queries make linear walks too slow.
Small-to-Large Merging (DSU on tree) + Tree DFS (Paths & Recursion)
When: distinct colours per subtree; dominant colour sums. Building a set per subtree naively is O(n2).
Tree BFS (Level Order)
- Binary Tree Level Order TraversalMedium
- Average of Levels in Binary TreeEasy
Tree DFS (Paths & Recursion)
- Maximum Depth of Binary TreeEasy
- Same TreeEasy
- Path SumEasy
BST Logic
- Insert into a Binary Search TreeMedium
- Range Sum of BSTEasy
Tree Construction & Serialization
- Convert Sorted Array to Binary Search TreeEasy
- Construct Binary Tree from Preorder and Inorder TraversalMedium
Further reading
- Binary Tree explore cardLeetCode
Traversals, top-down vs bottom-up recursion; core interview patterns.
- Tree cheatsheetTech Interview Handbook
BST properties, traversals and serialization problem list.
- BST visualizerVisuAlgo
Animate BST/AVL operations and traversals.
- Introduction to Tree AlgorithmsUSACO Guide
Trees as graphs: DFS on trees, subtree sizes, diameter.
- Lowest Common Ancestorcp-algorithms
LCA techniques beyond the interview version; binary lifting.
- NeetCode RoadmapNeetCode
Pattern-ordered problem tree with a short video per problem; best interview-first path.