List the Levels From the Bottom Up
Implement bottomUpLevels
You are given the root of a binary tree. Group the node values by level and return the groups starting from the deepest level and ending with the root's level; inside each group, keep the values in left-to-right order. For an empty tree return an empty list.
You can walk the tree repeatedly, once per level from the bottom, or collect the levels top-down with a queue in one pass and simply reverse the order of the rows.
Example 1:
Input: root = [17,8,25,4,12,null,30,2]
Output: [[2],[4,12,30],[8,25],[17]]
Example 2:
Input: root = [6,null,9]
Output: [[9],[6]]
Example 3:
Input: root = []
Output: []
+ 11 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes ≤ 100 - ●
−100 ≤ node.val ≤ 100 - ●
The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child - ●
Return one list per level, but starting with the DEEPEST level and ending with the root level. Inside each level, list the values from left to right. An empty tree gives an empty list
root =
[17, 8, 25, 4, 12, null, 30, 2]