Binary trees, BSTs, tries, DP-on-trees. The shape that shows up everywhere — file systems, DOM, expression parsers, search structures. Master the four traversals and tree-recursion template, and 80% of tree interview problems become mechanical.
Pre-order: root → left → right. In-order: left → root → right (sorted for BST). Post-order: left → right → root (used for delete/aggregate). Level-order: BFS, queue-based.
Solve children first; combine at the root. Most tree problems fit this template: recurse left, recurse right, do something with the results at the current node.
In-order traversal yields sorted output. Insert, search, delete all O(log n) when balanced. Self-balancing variants (AVL, red-black) usually NOT asked in interviews — but BST invariants are.
Each node is a character; root-to-leaf path is a word. Used for: autocomplete, word search, prefix matching, IP routing. Often the cleanest solution for ‘word starts with’ problems.
Compute a value per node based on subtree results. Diameter of tree, robber on tree, max-sum path. The recursion returns multiple values per node (state + answer).
Tree problems are won on recursion clarity — being able to articulate ‘what each call returns’. The AI coach grades that articulation explicitly.
Pre-order (root → left → right) — Used for: tree serialization, copying a tree, expression-tree evaluation in prefix notation.
In-order (left → root → right) — Used for: BST sorted output, validating a BST, kth smallest in BST.
Post-order (left → right → root) — Used for: deletion (delete children first), tree height, DP-on-trees, expression-tree evaluation in postfix.
Level-order (BFS, queue) — Used for: zigzag traversal, level averages, right side view, vertical order traversal.
Most tree problems fit one pattern. Learn this template once; reuse forever.
function solve(node):
if node is None:
return base_case
left_result = solve(node.left)
right_result = solve(node.right)
# combine left, right, and node.val
return combined_resultAlmost every tree problem boils down to: define base_case, define how left_result and right_result combine with node.val. Diameter, depth, balanced-check, path-sum, lowest-common-ancestor — all variations of this template.
Trie problems are usually disguised as string-search problems. Recognition cue: ‘starts with’, ‘prefix’, ‘autocomplete’, ‘word search in a board’.
Master the four traversals (pre/in/post/level-order) and the tree-recursion template. Once those are reflex, most tree problems fall into a small number of variations. Drill voice-paced — being able to articulate 'what each recursive call returns' is what wins tree interview rounds.
Recursion for almost everything. Iterative versions of in-order/pre-order/post-order using a stack come up occasionally — usually as a follow-up question after you've solved the recursive version. Iterative is harder to write under pressure; show the recursive solution first.
Binary tree: any tree with at most 2 children per node. BST: a binary tree where left < node < right. In-order traversal of a BST yields sorted output — that property unlocks many BST-specific problems (kth smallest, validate BST, search). General binary tree problems don't have that ordering shortcut.
Important enough to know cold. ~10% of FAANG coding rounds include a trie problem. The 'implement Trie' problem is a near-certain ask if interviewing at Meta. Memorize the implementation; recognize the 'prefix' cue.
Yes. PhantomCode's coach grades recursion clarity — can you articulate what each recursive call returns and how the parent combines results? That articulation is the single highest-leverage skill in tree interview rounds, and it doesn't show up in correctness alone.
Voice-paced practice with explicit recursion-articulation feedback. Same coach that runs in your real coding interview.
Start tree drillsDownload now — invisible, undetectable, and works on every platform. Plans start at $19.