Onion-Peel Layers of a Binary Tree
Updated
Problem statement
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
1and10000nodes. -10^9 <= Node.val <= 10^9.
More eBay Onsite Interview questions
- Extract Hashtags from a TweetONSITE INTERVIEW · Seen Nov 2026
- Validate Ada Integer LiteralONSITE INTERVIEW · Seen Nov 2026
- Count Distinct Pairs With Sum KONSITE INTERVIEW · Seen Nov 2026
- Diameter of Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Count Staircases with Strictly Increasing StepsONSITE INTERVIEW · Seen Oct 2026
- Hierarchical Permission CheckONSITE INTERVIEW · Seen Oct 2026
- Matrix MultiplicationONSITE INTERVIEW · Seen Oct 2026
- N-Queens Solution CountONSITE INTERVIEW · Seen Oct 2026
- Gas StationONSITE INTERVIEW · Seen Oct 2026
- Container With Most WaterONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Match a Descendant Selector in a DOM TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions