FrontendInterviews.dev

Loading problem…

109. Binary Tree Level Order Traversal

Medium•
Acceptance: 88.24%
•
🔓3/3 Pro unlocks today

You're building a tree visualization component that needs to display nodes level by level.

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

Requirements

  • Return an array of arrays, one inner array per level
  • Maintain left-to-right ordering within each level
  • If the tree is empty, return []

Examples

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Explanation:
Level 0: [3], Level 1: [9,20], Level 2: [15,7].

Example 2:

Input: root = [1]
Output: [[1]]
Explanation:
Single node tree.

Example 3:

Input: root = []
Output: []
Explanation:
Empty tree.

Constraints

  • 0 <= number of nodes <= 2000
  • -1000 <= Node.val <= 1000