102. Binary Tree Level Order Traversal

Binary Tree Level Order Traversal (LeetCode 102) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Level Order Traversal subpattern.

How to solve Binary Tree Level Order Traversal

Binary Tree Level Order Traversal is a Level Order Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master level order traversal using BFS. Solve zigzag, right side view, and level sum problems.

Work towards the solution in these steps:

  1. Use a queue to perform BFS.

The reference solution runs in O(N) time and O(N) space.

The Binary Tree Level Order Traversal solution and editorial walks through the full approach with reference code.

Problem statement

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]]

Example 2:

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

Example 3:

Input: root = [] Output: []

Constraints:

- The number of nodes in the tree is in the range [0, 2000].

- -1000 <= Node.val <= 1000

Topics and companies

Topics: Tree, Breadth-First Search, Binary Tree.

Reported in interviews at Bloomberg, Amazon, Facebook, Microsoft, LinkedIn, Adobe, Paypal, Google, Uber, Yahoo.

How to practise Binary Tree Level Order Traversal on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(N) space.

Open Binary Tree Level Order Traversal in the code editor, or read the Binary Tree Level Order Traversal editorial for a worked solution with its approach and complexity analysis.

Where Binary Tree Level Order Traversal sits in the DSA pattern sheet

Problems related to Binary Tree Level Order Traversal

Other problems that use the same Level Order Traversal and Tree Traversal Patterns (DFS & BFS) techniques:

Binary Tree Level Order Traversal FAQ

What is the LeetCode number of Binary Tree Level Order Traversal?

Binary Tree Level Order Traversal is LeetCode problem 102, rated Medium.

What pattern is used to solve Binary Tree Level Order Traversal?

Binary Tree Level Order Traversal is a Level Order Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Level Order Traversal subpattern.

What is the time complexity of Binary Tree Level Order Traversal?

The reference solution runs in O(N) time and O(N) space.

Which companies ask Binary Tree Level Order Traversal?

Binary Tree Level Order Traversal has been reported in interviews at Bloomberg, Amazon, Facebook, Microsoft, LinkedIn, Adobe, Paypal, Google, Uber, Yahoo.

Preparing this workspace.