47. Binary Tree Level Order Traversal

47. Binary Tree Level Order Traversal

Medium
55.0%
2.1k
bfstreebinary-treequeue
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large-scale platform organizes hierarchical data such as user permissions, category trees, service dependencies, or UI components using binary trees.

For analytics dashboards, UI rendering, and monitoring tools, the platform often needs to process the tree level by level, starting from the root and moving downward. This traversal helps visualize hierarchy depth, process nodes by priority, and perform breadth-based computations.

The platform needs a reliable way to traverse a binary tree in level order, collecting node values layer by layer.

This is a common practice coding problem for DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

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

Level order traversal means visiting nodes from left to right at each depth level, starting from the root.

Input Format

  • root: the root node of a binary tree

Each node is defined as:

TreeNode { int val; TreeNode left; TreeNode right; }

Output Format

  • A list of lists of integers
  • Each inner list contains the values of nodes at the same depth level

Examples

Example 1:

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

Example 2:

Input: root = [1]
Output: [[1]]

Example 3:

Input: root = []
Output: []

Constraints

  • 0 <= number of nodes <= 100,000
  • -10^9 <= Node.val <= 10^9

Loading editor...

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

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback