Binary Tree Level Order Traversal II
1 approach, code in every language
Problem description
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 <= 1000
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Reading A Binary Tree Deepest Level First is our own problem on the technique behind Binary Tree Level Order Traversal II: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free