Pattern · 21 problems
Binary Tree Interview Problems
Tree problems reward trust. I decide what a call returns for its subtree, then combine at the current node.
Free: write the code and run it, no sign-up.
When I reach for it
- Each node has children, and the answer depends on the subtrees.
- The statement says depth, height, ancestor or path.
- A binary search tree hands you an order for free.
Problems, easiest first
7 easy · 11 medium · 3 hard. Open one to read it, then write it in the free editor.
- Same TreeeasyBFS & DFS
- Subtree of Another TreeeasyStringsBFS & DFS
- Maximum Depth of Binary TreeeasyBFS & DFS
- Invert Binary TreeeasyBFS & DFS
- Balanced Binary TreeeasyBFS & DFS
- Diameter of Binary TreeeasyBFS & DFS
- Kth Largest Element in a StreameasyHeapsDesign
- Construct Binary Tree from Preorder and Inorder TraversalmediumBFS & DFSArrays & Hashing
- Implement Trie (Prefix Tree)mediumDesignStrings
- Kth Smallest Element in a BSTmediumBFS & DFS
- Lowest Common Ancestor of a Binary Search TreemediumBFS & DFS
- Design Add and Search Words Data StructuremediumDesignStrings
- Binary Tree Level Order TraversalmediumBFS & DFS
- Validate Binary Search TreemediumBFS & DFS
- Word BreakmediumDynamic ProgrammingStrings
- Binary Tree Right Side ViewmediumBFS & DFS
- Count Good Nodes in Binary TreemediumBFS & DFS
- Lowest Common Ancestor of a Binary TreemediumBFS & DFS
- Binary Tree Maximum Path SumhardDynamic ProgrammingBFS & DFS
- Serialize and Deserialize Binary TreehardDesignBFS & DFS
- Word Search IIhardBacktrackingStrings
The idea
Trust the recursion. Decide what one call returns for its subtree. Then combine the left answer and the right answer at the current node.
Ask two questions. What does this node need from its children? What does it pass up to its parent? Those two answers can differ.
def depth(node):
if node is None:
return 0 # an empty tree has depth 0
left = depth(node.left) # trust the call for each subtree
right = depth(node.right)
return 1 + max(left, right) # combine at this nodeWhere it goes wrong
- Skipping the base case for an empty node.
- Mixing up what a call returns with what you track on the side.
- Walking a whole binary search tree when its order lets you skip half.
Say this before you type. Say what a call returns for an empty tree, and what it returns for a node.
Where Coco comes in
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.
Questions people ask
- Should I write trees recursively or iteratively?
- Write the recursion first, since it matches the tree. Switch to an explicit stack if the interviewer worries about depth.
- Which traversal order should I use?
- Pre-order handles a node before its children. In-order gives sorted values from a search tree. Post-order lets children report first.
Meet these next
Fin and Coco are characters, not people. strongyes.io is independent. Leo builds it with AI agents.
