Binary Trees Dominate Coding Interviews
Binary tree leetcode problems make up roughly 20 percent of all coding interview questions — more than any other single data structure. If you study one topic thoroughly before your interview, trees should be it.
The reason trees appear so frequently is that they test multiple skills at once: recursion, depth-first and breadth-first thinking, edge case handling, and the ability to break a problem into subproblems. A single tree question can reveal how comfortable you are with all of these concepts.
The good news is that binary tree interview problems follow a small number of repeatable patterns. Once you learn the DFS template, the BFS template, and a handful of BST-specific tricks, you can solve the vast majority of tree questions you will encounter.
This guide walks you through every major tree pattern, from basic traversal orderings to advanced problems like Binary Tree Maximum Path Sum. By the end, you will have a mental framework for approaching any binary tree problem in an interview setting.
Tree Traversal Fundamentals: Inorder, Preorder, Postorder
Every binary tree problem starts with traversal — visiting nodes in a specific order. There are three classic depth-first orderings, and each one has distinct use cases that interviewers expect you to know.
Inorder traversal visits the left subtree, then the current node, then the right subtree. For a binary search tree, inorder traversal produces elements in sorted order. This single property is the foundation of several important BST problems.
Preorder traversal visits the current node first, then the left subtree, then the right subtree. Preorder is useful when you need to process a node before its children — for example, when serializing a tree or making a copy of it.
Postorder traversal visits the left subtree, the right subtree, and then the current node last. Postorder is the natural choice when the answer for a node depends on the answers from its children, such as calculating the height or diameter of a tree.
- Inorder (left, root, right) — gives sorted order for BSTs, used in Validate BST and Kth Smallest
- Preorder (root, left, right) — process parent before children, used in Serialize/Deserialize and tree construction
- Postorder (left, right, root) — process children before parent, used in Max Depth, Diameter, and tree deletion
- Each traversal can be implemented recursively in three lines or iteratively with an explicit stack
DFS Patterns on Binary Trees
Depth-first search is the workhorse of binary tree interview problems. The tree DFS patterns you need can be distilled into a single recursive template: handle the base case when the node is null, recurse on the left and right children, and combine or return the results.
Max Depth of Binary Tree (#104) is the simplest DFS problem and the best starting point. The base case returns 0 for a null node, and the recursive case returns 1 plus the maximum of the left and right subtree depths. This three-line solution captures the essence of every tree DFS problem.
Path Sum (#112) extends the template by threading a value through the recursion. At each node, you subtract the node value from the target sum. When you reach a leaf node with a remaining sum of zero, you have found a valid path. This "carry a value down" pattern appears in many tree problems.
Diameter of Binary Tree (#543) is a classic example of the "postorder with global variable" pattern. You compute the height of each subtree in postorder fashion, but at each node you also update a global maximum with the sum of left height plus right height. The diameter is the longest path between any two nodes, not necessarily through the root.
Lowest Common Ancestor (#236) uses a different DFS flavor. You recurse on both subtrees and check if the target nodes are found on the left side, the right side, or both. When both sides return a result, the current node is the LCA. This "information bubbling up" pattern is crucial for many advanced tree problems.
- Base case: if node is null, return the identity value (0 for depth, false for path sum, null for LCA)
- Recursive case: recurse on node.left and node.right, then combine results
- Max Depth (#104) — return 1 + max(left, right)
- Path Sum (#112) — carry remaining sum down, check at leaf nodes
- Diameter (#543) — postorder height computation with global max update
- LCA (#236) — recurse both sides, current node is LCA when both sides return non-null
Pro Tip
Most binary tree problems follow the same recursive template: handle the base case (null node), recurse on left and right children, combine results. Once you internalize this skeleton, tree problems become formulaic.
BFS and Level-Order Patterns
Breadth-first search on a binary tree means processing nodes level by level, from top to bottom and left to right. The binary tree BFS pattern uses a queue: start with the root, and for each node you dequeue, enqueue its left and right children.
Binary Tree Level Order Traversal (#102) is the foundational BFS problem. The key technique is processing one level at a time by recording the queue size at the start of each level, then dequeuing exactly that many nodes. This gives you a list of lists, where each inner list contains the values at one level.
Binary Tree Right Side View (#199) builds directly on level-order traversal. For each level, you only keep the last node — the rightmost one visible from the side. This is a one-line modification to the level-order template: instead of collecting all nodes per level, just track the last one.
Zigzag Level Order Traversal (#103) alternates the direction at each level. You can implement this by reversing every other level after doing a standard level-order traversal, or by using a deque and alternating which end you add children to. Both approaches are O(n).
BFS is the right choice whenever a problem mentions "level," "depth," "minimum distance," or "shortest path in an unweighted tree." If the problem asks for the minimum depth of a tree, BFS finds it the moment you encounter the first leaf node — no need to explore the entire tree like DFS would.
BST-Specific Patterns: Leveraging Sorted Order
Binary search tree problems are a distinct subcategory because BSTs have a structural property you can exploit: for every node, all values in the left subtree are smaller and all values in the right subtree are larger. This means inorder preorder postorder traversals each have special significance on BSTs.
Validate BST (#98) is the most important BST problem. The naive approach of checking each node against its direct children is wrong — a node must be less than everything in its right subtree, not just its right child. The correct approach passes a valid range (min, max) down the recursion, narrowing it at each step.
Kth Smallest Element in BST (#230) exploits the fact that inorder traversal of a BST gives sorted order. Simply perform an inorder traversal and return the kth element you visit. You can optimize this by stopping early once you have counted k elements rather than traversing the entire tree.
Lowest Common Ancestor of BST (#235) is simpler than the general tree version because you can use the BST property to navigate. If both target values are less than the current node, go left. If both are greater, go right. When the values split — one is smaller and one is larger — the current node is the LCA.
Search, insert, and delete operations on BSTs all follow the same navigation pattern: compare the target value to the current node and go left or right accordingly. These operations run in O(h) time where h is the tree height, which is O(log n) for balanced BSTs but O(n) in the worst case for skewed trees.
- Validate BST (#98) — pass a (min, max) range down the recursion, narrow at each node
- Kth Smallest (#230) — inorder traversal gives sorted order, count to k and stop
- LCA of BST (#235) — use BST property to navigate: both left, both right, or split means found
- BST search/insert/delete — compare and go left or right, O(log n) for balanced trees
Key Insight
BST inorder traversal always gives elements in sorted order — this single property is the key insight behind Validate BST (#98), Kth Smallest (#230), and Convert BST to Sorted Array.
Advanced Binary Tree Patterns
Once you have the fundamentals down, a set of harder binary tree problems test your ability to combine multiple techniques or think about trees in non-obvious ways. These problems frequently appear at senior-level interviews and contests.
Serialize and Deserialize Binary Tree (#297) requires you to convert a tree to a string and back. Preorder traversal with null markers is the cleanest approach: write each node value followed by its children, using a sentinel for null nodes. Deserialization reads the values in order and reconstructs the tree recursively.
Construct Binary Tree from Preorder and Inorder Traversal (#105) tests your understanding of how traversal orderings encode tree structure. The first element of preorder is always the root. Find that root in the inorder array — everything to its left is the left subtree, everything to its right is the right subtree. Use a hash map for O(1) lookups in the inorder array to achieve O(n) total time.
Flatten Binary Tree to Linked List (#114) asks you to rearrange a tree into a right-skewed linked list following preorder. The elegant in-place approach processes the tree in reverse postorder (right, left, root) and links each node to the previously processed node. This avoids using extra space beyond the recursion stack.
Binary Tree Maximum Path Sum (#124) is one of the hardest tree problems. A path can start and end at any node — it does not have to pass through the root. At each node, you compute the best path going through that node (left chain plus node plus right chain) and update a global maximum. But you return only the best single-side chain upward, because a path cannot fork. Mastering this "compute locally, return a constrained value upward" pattern unlocks several other hard tree problems.
Binary Tree Practice Strategy
With so many binary tree leetcode problems available, a structured practice order makes all the difference. Start with the three foundational problems: Max Depth (#104) for DFS, Level Order Traversal (#102) for BFS, and Validate BST (#98) for BST-specific thinking.
Once those feel automatic, work through the next tier: Path Sum (#112), Binary Tree Right Side View (#199), Kth Smallest in BST (#230), and Lowest Common Ancestor (#236). These problems each add one new twist to the base templates you have already learned.
For advanced preparation, tackle Serialize/Deserialize (#297), Construct from Preorder and Inorder (#105), and finally Binary Tree Maximum Path Sum (#124). Expect these to take two to three times longer than the earlier problems — that is normal.
The key to long-term retention is spaced repetition. Solving a tree problem once teaches you the approach, but reviewing it three days later, then a week later, and then a month later is what makes the pattern stick. YeetCode flashcards are built around exactly this principle — each card drills a specific pattern so you build lasting recall instead of temporary familiarity.
- 1Start with Max Depth (#104), Level Order (#102), and Validate BST (#98) to build your base templates
- 2Move to Path Sum (#112), Right Side View (#199), Kth Smallest (#230), and LCA (#236) for pattern variations
- 3Tackle advanced problems: Serialize/Deserialize (#297), Construct from Traversals (#105), Maximum Path Sum (#124)
- 4Use spaced repetition with YeetCode flashcards to review patterns at increasing intervals
Watch Out
Binary Tree Maximum Path Sum (#124) is one of the hardest tree problems — the tricky part is that the path can start and end at any node. Don't attempt this until you've mastered basic path sum problems.