#102

Binary Tree Level Order Traversal

MediumBFSHas vizTreeBFS
Related studio

Problem

Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

Example 1

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

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

Three levels visited left to right.

Example 2

Input: root = [1]

Output: [[1]]

Single node.

Example 3

Input: root = []

Output: []

Empty tree.

Constraints

  • The number of nodes in the tree is in the range [0, 2000].
  • -1000 <= Node.val <= 1000
Loading visualization…

Explanation

Binary Tree Level Order Traversal is a Medium interview problem tagged [Tree, BFS]. The winning approach is typically **BFS**.

Intuition

First time you reach a node in an unweighted graph is a shortest path.

Approach

Queue start node(s), expand neighbors level by level, mark visited.

  1. Initialize queue + visited.
  2. Process size-batched levels if needed.
  3. Enqueue unseen neighbors.
  4. Stop early if target found.

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateBFS

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n log n) or O(n)

Space: O(n)

Before coding, restate the problem, name the pattern (BFS), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #102 Binary Tree Level Order Traversal as understood when the pattern clicks.