Loading content...
You are given the root node of a generalized tree structure where each node can have an arbitrary number of child nodes (not just two as in binary trees). Your task is to traverse the tree layer by layer, collecting the values of all nodes at each depth level.
Return a list of lists, where each inner list contains the values of all nodes at the same depth, ordered from left to right as they appear in the tree. The first inner list should contain only the root's value, the second list should contain all values at depth 1, and so on.
The tree is serialized in a level-order format where each group of children is separated by a null marker. The null value acts as a delimiter indicating the end of one node's children before listing the next node's children.
If the tree is empty (root is null), return an empty list.
root = [1,null,3,2,4,null,5,6][[1],[3,2,4],[5,6]]The tree has 3 levels. Level 0 contains just the root node (1). Level 1 contains nodes 3, 2, and 4 (all children of root). Level 2 contains nodes 5 and 6 (children of node 3). Traversing each level from left to right gives us [[1],[3,2,4],[5,6]].
root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14][[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]This is a more complex tree with 5 levels. The root (1) is at level 0. Its children (2,3,4,5) are at level 1. Their descendants form levels 2, 3, and 4 respectively. Each level is collected left-to-right in the output.
root = [1,null,2,3][[1],[2,3]]A simple tree where the root node (1) has two children (2 and 3). The output consists of two levels: [[1],[2,3]].
Constraints