MEDIUMasked at 2 companies

Closest Leaf in a Binary Tree

A medium-tier problem at 47% community acceptance, tagged with Tree, Depth-First Search, Breadth-First Search. Reported in interviews at Databricks and 1 others.

Founder's read

You're given a binary tree and a target node, and you need to find the closest leaf to that node. Databricks and LinkedIn have both asked this one. On paper it sounds straightforward, but candidates often get tangled in direction: the closest leaf might be up and over to a totally different subtree, not just down. The acceptance rate sits at 47%, which means nearly half the people who see this blank out on the traversal logic or forget to consider paths that backtrack. If this problem hits your live OA and you freeze on how to efficiently search up the tree while tracking distances, StealthCoder solves it invisibly while the proctor watches something else.

Companies asking
2
Difficulty
MEDIUM
Acceptance
47%

Companies that ask "Closest Leaf in a Binary Tree"

If this hits your live OA

Closest Leaf in a Binary Tree 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 for the engineer who has done the work but might still blank with a webcam pointed at him.

Get StealthCoder
What this means

The trick is recognizing that you can't solve this with a simple downward DFS. You need to treat the tree as a graph where you can move in any direction. Build a parent pointer for every node using a first DFS, then run BFS from the target node to find the first leaf you encounter. Common failures: forgetting to mark visited nodes (you'll loop forever), treating the target node as a potential leaf (it usually isn't), or implementing DFS down only and missing leaves in sibling subtrees or the parent's other branch. The problem combines Tree, Depth-First Search, and Breadth-First Search in a way that trips up people who memorize template code. When you hit this live and the obvious approach doesn't work, StealthCoder runs the correct bidirectional traversal in seconds without breaking screen share.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Closest Leaf in a Binary Tree 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 for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Closest Leaf in a Binary Tree interview FAQ

Is the target node guaranteed to be in the tree?+

The problem assumes the target node exists. You don't need to handle not-found cases. Focus on finding the closest leaf from that starting point.

Can a leaf be the target node itself?+

Typically no. A leaf is a node with no children, and the target is the starting point. You're finding a different leaf closest to it.

Why is BFS better than DFS for this problem?+

BFS explores nodes layer by layer by distance. The first leaf you hit is guaranteed to be closest. DFS can find a leaf deep in one branch before exploring closer options in another direction.

How do I search upward in a binary tree efficiently?+

Build a parent pointer during an initial DFS traversal. Once you have parent pointers, treat the tree as an undirected graph and run BFS or DFS from the target, avoiding revisiting nodes.

What's the time complexity?+

Two passes: one DFS to build parent pointers (O(n)), one BFS to find the closest leaf (O(n) worst case). Overall O(n), linear in tree size. Space is O(h) for recursion plus O(n) for the parent map.

Want the actual problem statement? View "Closest Leaf in a Binary Tree" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.