Group Serialized Tree Values by Depth
Trees & GraphsAuto-graded · 19 test casesLast updated
Parse a LeetCode-style level-order array and return values grouped into nested lists by depth, preserving left-to-right order.
Problem
You are given a binary tree serialized as a level-order array. The first element is the root. Each non-null element, in order, claims the next two array positions as its left and right children. Null entries have no children and consume no slots. Trailing nulls may be omitted, so the array can end mid-level.
Return the tree's values grouped by depth: the first sublist holds depth-0 values, the second holds depth-1 values, and so on. Within each sublist, preserve left-to-right order.
Implement group_by_depth(serialized).
Constraints
- The array contains at most 100,000 elements.
- Elements are signed 32-bit integers or
None. - Return an empty list for an empty tree or a tree whose root is
None. - Value
0is a valid node value and must not be dropped.
Clarifying Questions
- Is a flat breadth-first list sufficient? No, group by depth.
- Must left-to-right order be preserved within each level? Yes.
- Can the tree be a single long chain (skewed)? Yes.
- Are child references shared or cyclic? No, it is a valid tree.
Examples
Example 1
Input: group_by_depth([3, 9, 20, None, None, 15, 7])
Output: [[3], [9, 20], [15, 7]]
Example 2
Input: group_by_depth([1])
Output: [[1]]
Example 3
Input: group_by_depth([1, 2, 3, None, 4, None, 5])
Output: [[1], [2, 3], [4, 5]]
Example 4
Input: group_by_depth([None])
Output: []
Hints
Hint 1
Recognize the input is a breadth-first serialization, not a node object.
Hint 2
Freeze the current level's size before reading its children.
Hint 3
Use `is not None` checks so value 0 survives.
Hint 4
Advance a single cursor forward through the array; never rescan or pop from the front.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat the array as a breadth-first serialization rather than building a tree. Maintain two lists: current holds the non-null values at the current depth, and next_level accumulates the next depth's values. A cursor pos walks forward through the array, reading exactly two child slots for each node in current.
For each level, freeze current first, then iterate over it. For every node, read the left slot (if in bounds) and the right slot (if in bounds), appending non-null values to next_level. After the level is processed, append current to the result and swap current = next_level. Stop when a level produces no children.
Why It Is Correct
By induction, current always holds exactly the depth-d nodes in left-to-right order and pos always points at the first child slot of the first node in current. Each node contributes two slots in order, so the next 2 * len(current) slots are precisely the children of depth-d nodes, left child before right child, parents left to right. Keeping only non-null values yields exactly the depth-(d+1) nodes in order. When pos exceeds the array length, remaining slots are implicit nulls and are safely skipped by the bounds checks. The loop terminates when a level has no non-null children, meaning the tree is exhausted.
Complexity
- Time: O(n), where n is the array length. Each slot is read exactly once.
- Space: O(w) working space, where w is the maximum level width. Only
currentandnext_levelare alive; the output is O(n) and required.
Edge Cases
- Empty array or root
None: return[]. - Value
0or negatives: handled byis not Nonechecks. - Duplicates: harmless since positions are tracked, not values.
- Trailing nulls trimmed (e.g.,
[1, 2]): bounds checks treat missing slots as null. - Skewed tree (e.g.,
[1, None, 2, None, 3]): each level has one node; no recursion depth risk.