Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. This means we traverse from left to right, level by level, but return the levels in reverse order - starting from the leaf level and ending at the root level.
Think of it like building a tree from the ground up - we want to see each floor of the tree, but starting from the bottom floor and working our way to the top!
Example: For a tree with root value 3, left child 9, and right child 20 (where 20 has children 15 and 7), we would return [[15,7], [9,20], [3]] - notice how the deepest level comes first.
Input & Output
Visualization
Time & Space Complexity
We traverse the tree h times (once for each level), and each traversal visits all n nodes, giving us O(n * h). In the worst case of a skewed tree, h = n, so O(nยฒ)
Recursion stack depth is O(h) for tree height, plus O(w) for storing the widest level where w โค n
Constraints
- The number of nodes in the tree is in the range [0, 2000]
- Node values are in the range [-1000, 1000]
- Follow-up: Can you solve this iteratively and recursively?