MEDIUMasked at 1 company

Find Leaves of Binary Tree

A medium-tier problem at 81% community acceptance, tagged with Tree, Depth-First Search, Binary Tree. Reported in interviews at LinkedIn and 0 others.

Founder's read

Find Leaves of Binary Tree is the kind of problem that looks straightforward until you realize you're supposed to return leaves grouped by height, not in traversal order. LinkedIn asks this enough that you'll see it in their interview loop. The acceptance rate sits at 81%, which sounds high until you hit the problem live and realize the naive DFS doesn't produce the right shape. If you blank on the height-based grouping trick during the OA, StealthCoder runs invisibly and surfaces the working solution in seconds.

Companies asking
1
Difficulty
MEDIUM
Acceptance
81%

Companies that ask "Find Leaves of Binary Tree"

If this hits your live OA

Find Leaves of 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 trap is thinking in-order or level-order traversal. You actually need to process the tree bottom-up, grouping leaves by their distance to the root. The insight: recursively remove leaves, collect them at each level of removal, and repeat until the tree is empty. Each recursive call returns the height of the subtree, which determines which group that node belongs to. Most candidates start with a standard DFS, realize it's not grouping correctly, and lose time rewriting. The pattern involves returning both the result list and tracking node heights simultaneously. StealthCoder handles the state management and recursion structure if you get stuck on the actual implementation during screen share.

Pattern tags

The honest play

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

Find Leaves of 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.

Find Leaves of Binary Tree interview FAQ

Is this still asked at LinkedIn?+

Yes. LinkedIn is the primary reported source. It appears in their tree and DFS rounds. The 81% acceptance rate suggests most candidates who see it either know the pattern or learn it quickly, but the grouping-by-height trick still catches people off guard live.

What's the key insight I'm missing?+

You're grouping by height, not order. Process the tree from leaves upward, removing one layer of leaves at a time, and record which group they belong to. Each recursive call returns the height of the current node, which acts as the list index where that node's value goes.

How does this relate to basic tree traversal?+

Standard DFS (in-order, pre-order, post-order) gives you node values in a linear sequence. This problem requires you to track node depth relative to the bottom of the tree, then bucket nodes by that depth. It's tree traversal plus a secondary grouping dimension.

What's the time and space complexity?+

Time is O(n) because you visit each node once. Space is O(h) for the recursion stack, where h is tree height, plus O(n) for the output. In a balanced tree, h is O(log n), but worst case it's O(n) for a skewed tree.

Can I solve this iteratively?+

Yes, but recursion is more natural for this problem because you need to compute heights bottom-up. Iterative approaches exist using explicit stacks and manual height tracking, but they're messier and more error-prone under interview pressure.

Want the actual problem statement? View "Find Leaves of 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.