cheetcode

53. Binary Tree Level Order TraversalMedium

Return the node values level by level from top to bottom.

Given the root of a binary tree, return its level order traversal.

That means collecting all node values level by level from left to right.

For the Cheetcode runner, trees are represented as level-order arrays with null placeholders for missing children.

Reason about the value returned from each subtree and the state passed down from ancestors. Empty children and single-node trees should be handled naturally.

Examples

Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Input: root = [1]
Output: [[1]]

Constraints

  • The number of nodes in the tree is in the range [0, 2,000].
  • -1,000 <= Node.val <= 1,000