472. Floors From the Bottom
A company chart is stored as a binary tree: the chief is root and every employee has at most a left and a right direct report. An auditor wants a report that starts from the most junior staff and works up to the chief. Employees are grouped by their distance from the chief, and the groups are listed from the deepest group up to the chief's own group. Inside a group the employees keep their natural left-to-right order.
Given root, return a list of lists where each inner list holds the values of one group in left-to-right order, and the inner lists are ordered from the deepest level to level 0. If root is empty, return an empty list.
Example 1
- Input:
- root = [9,20,4,null,13,7,2,16]
- Output:
- [[16],[13,7,2],[20,4],[9]]
- Explanation:
The levels are listed from the deepest to the root level, each one left to right: [[16],[13,7,2],[20,4],[9]].
Example 2
- Input:
- root = [15,null,8,null,3]
- Output:
- [[3],[8],[15]]
- Explanation:
The levels are listed from the deepest to the root level, each one left to right: [[3],[8],[15]].
Example 3
- Input:
- root = [2,-1,-6]
- Output:
- [[-1,-6],[2]]
- Explanation:
The levels are listed from the deepest to the root level, each one left to right: [[-1,-6],[2]].
Constraints
0 ≤ number of nodes ≤ 105
-1000 ≤ Node.val ≤ 1000
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms
Expected complexity
- Time
- O(n)
- Space
- O(n)