Find Leaves of a Binary Tree
Reported by candidates from LinkedIn's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
LinkedIn reported this one in September 2026, and the 10000-node cap is the detail that matters. Peeling leaves off the tree one round at a time and rescanning works on a toy example, then crawls on a skewed chain where every round removes one node. This is Find Leaves of a Binary Tree, a tree problem where each node's removal round comes from its height. If you've got an OA invite, learn the one-pass idea below. StealthCoder sits invisible on your screen as a safety net if you blank mid-assessment.
The problem
You are given the root of a binary tree. Imagine removing every current leaf at the same time, recording their values, and repeating until the tree is empty. Return one array per removal round. Within each round, values must appear in the same left-to-right order produced by a postorder traversal of the original tree. A node belongs to round 0 when it is an original leaf. Otherwise, its round is one more than the larger round of its children. Function findLeaves(root: TreeNode) → int[][] Examples Example 1 root = [1,2,3,4,5] return = [[4,5,3],[2],[1]] Nodes 4, 5, and 3 are removed first. Node 2 then becomes a leaf, followed by the root. Example 2 root = [1,null,2,null,3] return = [[3],[2],[1]] The right-skewed chain exposes one leaf per round. Constraints The tree contains between 1 and 10000 nodes. -10^9 <= Node.val <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: a node's round equals its height, where a leaf is 0 and every other node is 1 + max(height of left, height of right). Run one postorder DFS that returns the height and drops the node's value into result[height], adding a new list when the index equals the result size. Postorder gives you the required left-to-right order inside each round for free. That's O(n) time. The pitfall is simulating the removal literally. On a 10000-node chain that means 10000 passes, so O(n^2), and you also have to mutate the tree. Another slip is returning depth instead of height, which flips the grouping. Recursion depth on a chain can reach 10000, so know your language's stack limit. If you freeze live, StealthCoder is the hedge that hands you the height-based DFS while you stay in the driver's seat.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Find Leaves of a Binary Tree cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find leaves of binary tree. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass LinkedIn's OA.
LinkedIn reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Leaves of a Binary Tree FAQ
What's the trick to Find Leaves of a Binary Tree?+
Group nodes by height, not by simulated removal. Height of a leaf is 0, otherwise 1 plus the max of the children's heights. One postorder DFS computes the height and appends the node's value to the list at that index. No tree mutation needed.
How hard is this problem really?+
Medium, but it's easy once you see the height idea. The code is about ten lines. People lose time by actually deleting leaves and rescanning, which is slow on deep trees. If you know postorder returning a value, you're most of the way there.
Why does 10000 nodes matter here?+
It rules out repeating a full traversal per round. A skewed chain makes n rounds, so brute force is O(n^2), about 100 million node visits. The single-pass height approach is O(n) and safe. It also means recursion depth can hit 10000, so watch your stack.
Does the order inside each round matter?+
Yes. Values must follow postorder of the original tree. Since you visit left, then right, then the node, and append as you go, each round's list comes out in the right order automatically. Don't sort the values, that would break it.
How do I prepare for this in 48 hours?+
Write the height-returning postorder DFS from memory twice. Then trace [1,2,3,4,5] by hand and confirm you get [[4,5,3],[2],[1]]. Test the skewed chain and a single node. Related tree problems like max depth use the same return-a-height habit.