Group Hierarchy Members by Depth Level
Trees & GraphsAuto-graded · 18 test casesLast updated
Construct an adjacency graph from reporting links and perform a level-order traversal to group node IDs by depth.
Given a collection of structural links within an organization, organize the members into lists corresponding to their depth level from the top-level leader (the node without a superior).
Each input element is represented as a pair [member_id, superior_id].
- A top-level leader (root) has
superior_idset toNone(ornull). There is guaranteed to be exactly one root node. - Every other member reports directly to exactly one superior.
Return a list of lists, where the outer list represents levels starting at depth 0 (containing only the root), depth 1 (the root's direct reports), depth 2 (reports of depth 1 members), and so forth.
Members in the same output level should preserve the relative order in which their reporting relationships appeared in the input list.
Constraints
n(total number of members) is between 1 and 10,000.- Each member ID is a unique integer.
- Exactly one pair in the input has a
superior_idofNone. - The reporting hierarchy forms a single valid tree structure with no cycles.
Examples
Example 1
Input: get_org_levels([[1, None], [2, 1], [3, 1], [4, 2]])
Output: [[1], [2, 3], [4]]
Example 2
Input: get_org_levels([[10, None], [20, 10], [30, 10], [40, 20], [50, 30]])
Output: [[10], [20, 30], [40, 50]]
Hints
Hint 1
The provided pairs describe child-to-parent relationships. You need to reverse these to traverse downward from parent to children.
Hint 2
Identify the root node where superior_id is None to start your traversal.
Hint 3
Use a level-by-level Breadth-First Search (BFS) to process each level as a list of IDs before moving to their reports.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem asks us to group nodes in a hierarchy by their distance (depth level) from the single root node.
-
Build a Top-Down Mapping: The input pairs specify
[child, parent]. We invert this to map eachparentto a list of its directchildren. While iterating through the input pairs, we also identify the single root node—the one whose parent isNone. -
Level-Order Traversal (BFS): Starting with a list containing just the root node (
[root]), we iteratively collect all children of the nodes in the current level to form the next level list. We repeat this process until no nodes remain for the next level. -
Order Preservation: By appending children into lists in the order they appear in the input and maintaining that processing order during BFS, we preserve the exact deterministic order required.
Complexity
- Time Complexity: $\mathcal{O}(N)$ where $N$ is the number of members. We iterate over the input array once to build the mapping ($\mathcal{O}(N)$ time) and then visit each node and edge exactly once during BFS ($\mathcal{O}(N)$ time).
- Space Complexity: $\mathcal{O}(N)$ auxiliary space to store the mapping dictionary, the level lists, and the final output.
Python Reference Solution
from typing import List, Optional
def get_org_levels(relationships: List[List[Optional[int]]]) -> List[List[int]]:
root = None
children = {}
for employee_id, manager_id in relationships:
if manager_id is None:
root = employee_id
else:
children.setdefault(manager_id, []).append(employee_id)
levels = []
current_level = [root]
while current_level:
levels.append(current_level)
next_level = []
for employee_id in current_level:
next_level.extend(children.get(employee_id, ()))
current_level = next_level
return levels