FastPrepBreadth-First Values of a Binary Tree

Breadth-First Values of a Binary Tree

Figma logoFigma● EasyINTERNONSITE INTERVIEW
Learn

Problem statement

Given the root of a binary tree, return its node values in breadth-first order.

Visit nodes level by level from top to bottom, and from left to right within each level. Return an empty array for an empty tree.

Function

breadthFirstValues(root: TreeNode) → int[]

Examples

Example 1

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

The traversal visits the root, then 9 and 20, then 15 and 7.

Example 2

root = [1,2,3,4,5]return = [1,2,3,4,5]

Values on the same level are emitted from left to right.

Example 3

root = []return = []

An empty tree has no values to visit.

Constraints

  • The tree contains at most 100000 nodes.
  • Each node value fits in a signed 32-bit integer.

More Figma problems

See Figma hiring insights
public int[] breadthFirstValues(TreeNode root) {
    // Write your code here.
}
root[3,9,20,null,null,15,7]
expected[3,9,20,15,7]
Checking account…