Binary Tree Level Order Traversal

Given the root of a binary tree, return the level order traversal of its nodes' values as a list of levels. Pattern focus: BFS Level Order with Queue. Process nodes level by level so each node is visited once and grouped by depth.

Input Format

root = binary tree root

Output Format

level order traversal as list of levels

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • root must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [3,9,20,null,null,15,7]

Output:

[[3],[9,20],[15,7]]

Explanation:

Nodes are grouped level by level from top to bottom.

Example 2:

Input:

root = [1]

Output:

[[1]]

Explanation:

A single node forms one level.

Loading...
Binary Tree Level Order Traversal - Queue Deque