Pattern

Trees

Binary tree problems built on traversal order, structural recursion, and reconstruction from encoded forms.

What it is

This is a category, not one technique: problems where the input or output is a binary tree and the interesting work is choosing the right traversal order (preorder, inorder, postorder, or level-by-level) and the right shape of recursion — some problems want a value bubbled up from the leaves, others want state pushed down from the root, and reconstruction problems want you to run a traversal backwards. A file-system-style tree (directories and files) shows up too, tested the same way: pick the right walk and keep path/child bookkeeping in one place.

When it applies

Tells in the problem

The classic mistake

Reconstruction problems (preorder+inorder, or a serialized string) invite an O(n) linear scan through the array to find the root's position in inorder on every recursive call, which turns an O(n) rebuild into O(n²) on skewed trees. The fix is a single up-front index of value to position, or slicing by explicit boundaries instead of value search. Traversal problems have a parallel mistake: writing the level-order or vertical-order walk with implicit ordering assumptions (assuming children are visited left-to-right by construction) that break the moment two nodes tie on depth and column — the fix is to make the tie-break rule explicit in how you enqueue or sort, not rely on incidental iteration order.

Questions to start with

All 17 trees questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.