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 traversing from left to right, level by level from leaf to root.

In other words, collect all nodes at each level, but return the levels in reverse order - starting from the deepest level and ending at the root level.

For example, if a tree has 3 levels, return the nodes in order: Level 3 → Level 2 → Level 1

Input & Output

Example 1 — Basic Tree
$ Input: root = [3,9,20,null,null,15,7]
Output: [[15,7],[9,20],[3]]
💡 Note: Level order traversal gives [[3],[9,20],[15,7]]. Reversing gives [[15,7],[9,20],[3]] for bottom-up order.
Example 2 — Single Node
$ Input: root = [1]
Output: [[1]]
💡 Note: Tree has only one level with one node. Result is [[1]].
Example 3 — Empty Tree
$ Input: root = []
Output: []
💡 Note: Empty tree returns empty result.

Constraints

  • The number of nodes in the tree is in the range [0, 2000]
  • -1000 ≤ Node.val ≤ 1000

Visualization

Tap to expand
Binary Tree Level Order Traversal IIInput Tree3920157Regular Level OrderLevel 0: [3]Level 1: [9,20]Level 2: [15,7]Bottom-Up OrderLevel 2: [15,7]Level 1: [9,20]Level 0: [3]Final Output:[[15,7], [9,20], [3]]✓ Levels reversed: deepest first, root last
Understanding the Visualization
1
Input Tree
Binary tree with nodes at different levels
2
Level Collection
Group nodes by their depth level
3
Bottom-Up Output
Return levels in reverse order
Key Takeaway
🎯 Key Insight: Bottom-up traversal = regular level-order + reverse the levels
Asked in
Amazon 42 Microsoft 35 Facebook 28 Apple 22
98.7K Views
Medium Frequency
~15 min Avg. Time
3.4K 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