Binary Tree Level Order Traversal II - Problem

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

example_1.py โ€” Standard Binary Tree
$ Input: [3,9,20,null,null,15,7]
โ€บ Output: [[15,7],[9,20],[3]]
๐Ÿ’ก Note: The tree has 3 levels: Level 0 (root) contains [3], Level 1 contains [9,20], and Level 2 (deepest) contains [15,7]. Bottom-up traversal returns levels in reverse order: deepest first, root last.
example_2.py โ€” Single Node Tree
$ Input: [1]
โ€บ Output: [[1]]
๐Ÿ’ก Note: Tree has only one node (the root), so there's only one level. The bottom-up traversal returns a single level containing just the root value.
example_3.py โ€” Empty Tree
$ Input: []
โ€บ Output: []
๐Ÿ’ก Note: Empty tree has no nodes, so the traversal returns an empty list. This is an edge case that should be handled gracefully.

Visualization

Tap to expand
Floor 0 (Root): [3]Floor 1: [9, 20]Floor 2 (Leaves): [15, 7]BFS processestop to bottom[15, 7][9, 20][3]Result Stack(newest on top)๐ŸŽฏ Key Insight: Deque PrependingInstead of append + reverse, we use deque.appendleft() or result.unshift()Each level gets prepended to the front, naturally creating bottom-up orderTime: O(n) | Space: O(w) where w is maximum tree width
Understanding the Visualization
1
Start at Ground Floor
Begin BFS at root level, queue contains [3]
2
Visit Floor 1
Process children of root: queue becomes [9, 20]
3
Visit Floor 2
Process leaf level: queue becomes [15, 7]
4
Stack Reports
As we process each floor, we stack the reports with newest on top
5
Read Stack
Final result reads from top to bottom: [[15,7], [9,20], [3]]
Key Takeaway
๐ŸŽฏ Key Insight: BFS naturally processes levels top-to-bottom, but we want bottom-to-top output. By using a deque and prepending each level to the front, we reverse the order during construction rather than after completion, making the solution both elegant and efficient.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

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ยฒ)

n
2n
โš  Quadratic Growth
Space Complexity
O(h)

Recursion stack depth is O(h) for tree height, plus O(w) for storing the widest level where w โ‰ค n

n
2n
โœ“ Linear Space

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?
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
98.0K Views
High Frequency
~15 min Avg. Time
2.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen