Loading...
Loading...
Given the root of a binary tree, return its node values level by level, with one array per level, starting from the root's level. Each array holds the values of every node on that level.
The reading direction alternates from one level to the next. The root's level is read left to right, the next level right to left, the level after that left to right again, and so on.
Example 1: each level is read in the opposite direction from the one above it.
If the tree is empty, return an empty array.
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.
Node.val ≤100The root level is just 3. The second level holds 9 and 20, read right to left as 20, 9. The third level holds 15 and 7, read left to right again.
A single node forms one level, read left to right.
The empty tree has no levels.