Trains the technique from
LeetCode 107Binary Tree Level Order Traversal IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A binary tree arrives as nodes, a flat list written down by walking the tree one level at a time from left to right.
nodes[0] holds the root's value.null means the child is absent. An absent child is not a node, so it claims no slots of its own further down the list.null.So [4, null, 6, 2] describes a root of 4 whose left child is absent, whose right child is 6, and whose grandchild 2 is the left child of 6.
Return the values of the tree grouped by level, with the deepest level first and the root's level last. Within each group the values run left to right. For a tree with no nodes return an empty list.
Example 1
The root is `5`, its left slot is `null` and its right child is `8`. The next two slots belong to `8`, giving it a left child `1` and a right child `4`. Taking the levels from the deepest upward gives `[1, 4]`, then `[8]`, then `[5]`.
Example 2
The root `6` has children `2` and `9`. The four slots of the level below belong to `2` and then `9`, and the list supplies only two of them, so `2` has no left child and a right child `7` while `9` has none. The deepest level is `[7]`, above it `[2, 9]`, and the root's level last.
Example 3
The tree is a single root with both child slots left off the end of the list, so there is one level to report.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def levels_deepest_first(nodes: list) -> list[list[int]]:public List<List<Integer>> levelsDeepestFirst(Integer[] nodes)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.