Problem · Tree

Binary Tree Level-Order Traversal by Levels

Learn this problem
EasyAdobe logoAdobeFULLTIMEONSITE INTERVIEW

Problem statement

Given the root of a binary tree, return its values in breadth-first level order as a two-dimensional array.

  • The outer array is ordered from the root level downward.
  • Each inner array contains one level from left to right.
  • Return an empty array when root is null.

Function

levelOrderByLevels(root: TreeNode) → int[][]

Examples

Example 1

root = [3,9,20,null,null,15,7]return = [[3],[9,20],[15,7]]

The queue visits the root first, then values 9 and 20, and finally values 15 and 7.

Example 2

root = []return = []

An empty tree has no levels.

Constraints

  • 0 <= number of nodes <= 100000.
  • Every node value fits a signed 32-bit integer.
  • The input is a finite, acyclic binary tree.

More Adobe problems

drafts saved locally
public int[][] levelOrderByLevels(TreeNode root) {
    // TODO: return one row per breadth-first level.
}
root[3,9,20,null,null,15,7]
expected[[3],[9,20],[15,7]]
checking account