Binary tree interview problems
Master binary tree interview problems with this 6 pattern framework. 15 curated problems with the key insight for each.
Why 15 problems cover the full binary tree interview space
Which 6 traversal patterns every tree problem maps to
The key insight and difficulty for each of the 15 problems
How to sequence this list for maximum pattern coverage
Most engineers build their tree problem list by sorting LeetCode's tree tag by acceptance rate. They end up practicing 30-40 problems that happen to be popular, with no pattern coverage and no progression logic. Weeks of grinding, and binary tree interview problems still feel unpredictable.
More problems won't fix that. Every tree question maps to one of 6 traversal patterns, and once you see the groupings, 15 problems give you more coverage than 40 random ones.
Why 6 patterns cover all binary tree interview problems
Binary tree interview problems cluster around six traversal patterns: preorder, postorder, root-to-leaf, level order, lowest common ancestor, and simultaneous traversal. Two problems per pattern gets you to 15 total, and that's enough for full interview coverage. Compare that to the 40+ most engineers attempt.
Each pattern moves information through the tree differently. Preorder carries data downward. Postorder carries it upward. Level order processes nodes layer by layer. Recognize the mechanism, and the implementation follows from the pattern, not from memory.
Most "top 15 tree problems" lists online are sorted by popularity or difficulty. That tells you nothing about why these problems are related or how solving one prepares you for another. Engineers who grind 40 tree problems still freeze on unfamiliar ones because they've practiced random problems, not patterns. Group by pattern instead, and the skills from problem 1 directly transfer to problem 2 in the same group.
Preorder & postorder: The traversal core
Preorder and postorder are the two directions information flows in a binary tree. Preorder pushes data down from parent to child. Postorder computes data up from child to parent. Between the two, they account for most binary tree interview problems at Google, Amazon, and Meta.
Both patterns come in two flavors: stateless (the function's return value carries the answer) and stateful (an external variable tracks the real answer while the function returns something else). That split matters more than you'd expect. In stateless problems, the return value is the answer. In stateful problems, you track the answer somewhere else while the function returns supporting information. Almost every medium and hard tree problem is stateful.
1. Preorder
Start with Sum of path, a stateless preorder problem. Pass the remaining target sum downward at each node, check if the remainder is zero at a leaf. The function parameter carries all the state.
Right view is where stateful preorder shows up. You pass current depth downward, but a global record of the deepest level seen so far determines whether each node qualifies as the rightmost at its depth. That external state tracker is what makes it stateful. This one comes up often in Amazon and Meta interviews.
2. Postorder
Height of binary tree is the stateless postorder starting point. Each node returns 1 + max(left height, right height). Solve this first because every stateful postorder problem builds on the same bottom-up return mechanism.
Diameter of tree is where the stateful postorder pattern clicks. The longest path through any node equals its left height plus its right height, but the function still needs to return height so its parent can compute correctly. You track the actual answer in a separate variable while the recursion returns something else.
Python
That's the stateful postorder pattern in two lines.
The function returns height, but the answer lives in max_diameter. That split between "what the recursion returns" and "what you actually need" shows up in distribute coins, longest monotonic path, and a dozen other tree problems.
“Stateful postorder separates what the recursion returns from what you actually need. That distinction unlocks half of all medium and hard binary tree problems.”
Distribute coins uses the same stateful postorder structure in a different context. Each node holds some number of coins, and the goal is exactly one coin per node. The postorder return tracks surplus or deficit at each subtree while the global variable tracks total moves required. Same split, different domain.
Longest monotonic path pushes stateful postorder to its limit. You track both the longest increasing and decreasing chain at each node, updating a global maximum as you go. Google likes this one because managing multiple stateful trackers at the same time is genuinely tricky, and the problem exposes whether your postorder instincts are solid or just memorized from diameter.
Root to leaf & level order: Paths and layers
Root to leaf patterns descend recursively, building up state along each path. Level order uses a queue to process nodes layer by layer.
1. Root to leaf
Root to leaf paths asks you to enumerate every path from root to leaf. You build a path list as you descend and copy it when you hit a leaf. The catch: the path parameter is shared across recursive calls, so you need to backtrack (remove the last element) after returning from each child. Missing the backtrack step is the most common mistake on this problem, and it produces incorrect output that's hard to debug because the paths look almost right.
Equal paths determines if any root-to-leaf path sums to a target. It looks similar to Sum of Path from the preorder group, but there's a subtle difference: the root-to-leaf formulation forces you to check the condition only at leaves. That constraint matters when the tree contains negative values, because an internal node matching the target doesn't guarantee a valid path exists below it. Miss that distinction and your solution passes most test cases but fails edge cases with negative nodes.
2. Level order
Level order works differently from the recursive patterns above. Instead of depth-first descent, you process every node at one level before moving to the next using a queue. The problems feel different because you're managing queue state rather than recursive call state, and the thinking shifts from "what should this node return?" to "what should I do with all nodes at this depth?" If you've worked through BFS vs DFS traversal mechanics, the queue-based processing will feel familiar.
Level sum is where you start. Sum the values at each level by processing all nodes at the current depth before advancing. This basic BFS skeleton reappears in every harder level order variant, so get comfortable with it.
Zigzag traversal alternates left-to-right and right-to-left at each level. The modification is straightforward (reverse the order at every other level), but interviewers want to hear why the standard BFS order needs the adjustment and whether you'd reverse the output or alternate insertion order.
Vertical traversal groups nodes by column, ordered by row within each column. You map each node to (column, row) coordinates and sort by column. Amazon and Meta ask this one often because it tests whether you can layer a secondary data structure on top of BFS without losing track of traversal order. The coordinate mapping is conceptually simple, but getting the sorting right when two nodes share the same (column, row) position trips people up.
Lowest common ancestor & simultaneous traversal: Multi node reasoning
These patterns ask about relationships between nodes, not properties of individual nodes. FAANG interviews lean on them because they test whether you can break a multi-node problem into recursive subproblems.
1. Lowest common ancestor
Lowest common ancestor is the core problem. Given two target nodes, you find their deepest shared ancestor by checking whether both targets appear in different subtrees of each node. If they do, that node is the LCA. If both are in the same subtree, the LCA is deeper. The identification lesson for this pattern teaches three structural signals that distinguish LCA problems from standard postorder problems.
Distance between nodes builds directly on LCA. Find the lowest common ancestor first, then compute the depth of each target relative to the LCA. Total distance equals depth(node1) + depth(node2). Interviewers like this as a follow-up because it tests whether you can compose two simple operations (LCA + depth) into one solution.
2. Simultaneous traversal
Identical trees is the simplest form: traverse both trees at the same time, comparing nodes at each step. It's also a building block for harder variants.
Subtree detection checks whether one tree is a subtree of another. For every node in the main tree, you check if the subtree rooted there is identical to the candidate. This nests simultaneous traversal inside a preorder scan. FAANG phone screens love it because it tests two patterns in one problem.
How to practice these binary tree interview problems
Don't practice these 15 in difficulty order. The postorder problems won't make sense until you've internalised how preorder works. LCA builds on postorder return-value mechanics. Simultaneous traversal assumes you're comfortable with single-tree recursion.
Follow the pattern order: preorder, then postorder, then root-to-leaf, level order, LCA, and simultaneous traversal. Within each group, solve the direct application first (height before diameter, identical trees before subtree detection). If you can't trace the solution mentally before running it, the pattern hasn't clicked yet.
Once you've worked through each group, interleave problems from different patterns during review rather than grinding one pattern exhaustively. That's what trains you to identify which pattern a new problem needs, and that identification ability is the difference between passing a tree round and just recognizing solutions you've seen before.
What about BST, heap, or trie problems? Those use different patterns (sorted traversal, top-K elements, prefix matching) that don't share the same traversal mechanics, so they deserve their own focused lists. This one covers binary tree traversal patterns only because FAANG interviewers treat them as a self-contained unit.
Codeintuition's learning path sequences all 16 courses in prerequisite order. The Binary Tree course alone covers 62 problems across these same 6 patterns. If you want to see whether structured pattern training changes how you approach tree problems, the free tier gives you two complete courses (Arrays and Singly Linked List) with 63 lessons and 15 patterns, all taught through the understand-identify-apply sequence. Start there and see if it clicks before committing.
The real test isn't how many tree problems you've solved. It's whether you can look at a new one and name which of the 6 patterns applies before opening the hints.
Do you want to master data structures?
Try our data structures learning path made of highly visual and interactive courses. Get hands on experience by solving real problems in a structured manner. All resources you would ever need in one place for FREE