Smallest Subtree with all the Deepest Nodes
A medium-tier problem at 72% community acceptance, tagged with Hash Table, Tree, Depth-First Search. Reported in interviews at Salesforce and 0 others.
You're asked to find the smallest subtree containing all the deepest nodes in a binary tree. Salesforce has asked this. The trap is obvious: many candidates traverse once to find max depth, then traverse again hunting for those nodes, and still return the wrong subtree because they don't track which ancestor actually contains all of them. You need the deepest leaves, but you need the minimal ancestor that spans all of them. If you blank on the two-pass or postorder trick during your OA, StealthCoder runs invisible during screen share and surfaces the working solution in seconds.
Companies that ask "Smallest Subtree with all the Deepest Nodes"
Smallest Subtree with all the Deepest Nodes is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.
Get StealthCoderThe key insight is that the answer isn't just "find the deepest nodes" but "find the ancestor of minimum height that holds all of them." Most candidates either depth-first-search once (incomplete) or miss that you need to compare subtrees at the same depth during traversal. The clean solution uses DFS with a return value that tracks both the deepest height in each subtree and the candidate node itself. You traverse post-order, building up information from leaves, so when you're evaluating a node, you already know whether its left and right children hold deepest nodes. If both subtrees contain deepest nodes at the same max depth, the current node is your answer. If only one does, bubble that result up. This is a postorder pattern combined with depth tracking. The acceptance rate of 72% suggests many candidates get close but fumble the return logic or mix up when to return the current node versus a child.
Pattern tags
You know the problem.
Make sure you actually pass it.
Smallest Subtree with all the Deepest Nodes recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Smallest Subtree with all the Deepest Nodes interview FAQ
What's the trick that separates AC from WA on this one?+
You must return the minimal ancestor that contains all deepest nodes, not the nodes themselves. Use postorder DFS tracking both the max depth in each subtree and the candidate ancestor. If both children have the same max depth, the current node is the answer. If only one child has deepest nodes, return that subtree's result up.
Do I need two passes to solve this?+
No, one postorder pass works. During recursion, each node learns the max depth of its subtrees. You bubble up the candidate ancestor as you return. Two passes (find depth first, then find node) is slower and error-prone; the single-pass postorder approach is cleaner and matches the 72% acceptance.
How does depth-first-search help here versus breadth-first-search?+
DFS lets you return information bottom-up via the return value, so you know subtree depths before deciding if a node is the answer. BFS finds depth layers but forces separate logic to track ancestors. DFS is the natural fit for this postorder-ancestor problem.
What if all deepest nodes are in only the left or right subtree?+
Return the subtree's result, not the current node. Only return the current node if both left and right children have nodes at the same maximum depth. If one subtree is deeper, that subtree's result (an ancestor within it) is the answer.
Is this the same as 'Lowest Common Ancestor of Deepest Leaves'?+
Yes, often phrased identically. The problem asks for the smallest subtree rooted at the deepest LCA. Hash table isn't needed unless you store depth results in a map, but a recursive return value is simpler. Salesforce's version likely expects the postorder DFS approach without extra space.
Want the actual problem statement? View "Smallest Subtree with all the Deepest Nodes" on LeetCode →