Given the root of a binary tree, produce the values of its nodes grouped by depth, but ordered from the bottom level up to the top.
Concretely, the first inner list should contain the deepest (leaf) level read left to right, the next list the level above it, and so on, ending with the list that holds just the root. This is the ordinary level-order (breadth-first) grouping with the outer list reversed.
Example 1
Input: root = [3,9,20,null,null,15,7]
Output: [[15,7],[9,20],[3]]
Top-down the levels are [3], [9,20], [15,7]. Reading them from the bottom up yields [[15,7],[9,20],[3]].
Example 2
Input: root = [1]
Output: [[1]]
A single node forms one level, and reversing a one-level list changes nothing.
Constraints
The number of nodes in the tree is in the range [0, 2000].-1000 <= Node.val <= 1000See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI