Tree Level by Level
You are given the root of a binary tree. Return its values level by level: a list for each level, from the root's level down, with each level's values from left to right. An empty tree has no levels.
Trees in the examples are written level by level, left to right, with null where a child is missing: [1,null,2] is a root 1 with only a right child, 2. [] is an empty tree, with no root at all.
Constraints
- 0 ≤ number of nodes ≤ 10,000
- -1,000 ≤ node value ≤ 1,000
- The tree is at most 1,000 levels deep.
Example 1
Input
root = [3,9,20,null,null,15,7]
Output
[[3],[9,20],[15,7]]
Why:
The root 3, then its children 9 and 20, then 20's children 15 and 7.
Example 2
Input
root = [1]
Output
[[1]]
Why:
One node, one level.
Example 3
Input
root = []
Output
[]
Why:
An empty tree has no levels.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –