Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Level order traversal and network delay



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Binary Tree Level-wise Traversal

Binary Tree Level-wise Traversal

Problem Statement

Given a binary tree, produce the level-wise traversal of its node values. That is, return the values of nodes grouped by each level, from left to right.

For instance, consider the binary tree represented as [5,3,8,null,null,7,10]:

    5
   / \
  3   8
     /  \
    7    10

Your task is to return the values grouped by levels as follows:

[
  [5],
  [3, 8],
  [7, 10]
]

Examples

// Example 1:
Input: root = [1, 2, 3, 4, 5, null, 7]
Output:
[
  [1],
  [2, 3],
  [4, 5, 7]
]

// Example 2:
Input: root = [10]
Output:
[
  [10]
]

// Example 3:
Input: root = []
Output:
[]

Constraints

  • The number of nodes in the tree is between 0 and 1500.
  • Node values lie within the range -1000 to 1000.