Given the root of a binary tree, return the level-order traversal of its node values: one array per depth level, from the root level downward, with each level's values listed left to right.
The empty tree yields an empty result.
Tree Encoding
Your function receives root as a TreeNode (or as null for the empty tree). The node type is provided for you, with val, left, and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the input is displayed; the decoding is done for you.
Constraints
The number of nodes in the tree is in the range [0,2000].
−1000≤Node.val≤1000
Examples
Example 1
Input
root = [3, 9, 20, null, null, 15, 7]
root
Output
[[3], [9, 20], [15, 7]]
Explanation
3 is the root; 9 and 20 form the second level; 15 and 7 are the children of 20 and form the third level.
Example 2
Input
root = [1]
root
Output
[[1]]
Example 3
Input
root = []
Output
[]
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up