Peel Leaves From an Org Chart Tree Layer by Layer

Last updated: November 29, 2026

Quick Overview

Given a binary tree representing a reporting chart, repeatedly remove all current leaves and record them, until the tree is empty. Return the list of layers, each in left-to-right order, without actually mutating the tree.

LinkedIn
Coding & Algorithms
Software Engineer
LinkedIn
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a binary tree representing a reporting chart, repeatedly remove all current leaves and record them, until the tree is empty. Return the list of layers, each in left-to-right order, without actually mutating the tree.

A long-running favorite at LinkedIn screens. The interviewer wants to see you notice that a node's removal round equals its height from the bottom, so a single postorder DFS solves it in linear time.

What the Interviewer Expects
  • Identifies that the layer index is the node's height (leaf = 0)
  • Uses one postorder traversal returning height
  • Appends each node value to result[height]
  • Avoids repeated scans that would be O(n^2)
  • Handles an empty tree
Key Topics to Cover
binary tree
postorder DFS
height
recursion
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you do this iteratively to avoid recursion limits on deep charts?
  • What if the chart is an n-ary tree?
  • How would you instead return layers by depth from the root?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: root of a binary tree, nodes with val, left, right. Output: list of lists. Layer 0 is the original leaves, layer 1 is nodes that become leaves after layer 0 is removed, and so on. Left-to-rig...

High-Level Design

A node is removed in round r exactly when its tallest child was removed in round r-1, so its round is 1 + max(height(left), height(right)) with missing children at height -1. A postorder DFS compute...


Submit Your Answer
Markdown supported