FastPrepLinked List of Binary Tree Nodes at a Given Level

Linked List of Binary Tree Nodes at a Given Level

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Collect the binary-tree nodes at zero-based depth level from left to right, as if appending them to a new singly linked list. Return that list's values. Return empty when the level is absent.

Function

nodesAtLevel(root: TreeNode, level: int) → int[]

Examples

Example 1

root = [1,2,3,4,5,null,6]level = 2return = [4,5,6]

The depth-two nodes appear left-to-right as 4,5,6.

Constraints

  • The tree contains at most 10^5 nodes.
  • level >= 0.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] nodesAtLevel(TreeNode root, int level) {
  // Write your code here.
}
root[1,2,3,4,5,null,6]
level2
expected[4,5,6]
Checking account…