FastPrepLargest Value in Each Tree Row

Largest Value in Each Tree Row

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given the root of a binary tree, return the largest node value at every depth, ordered from the root level downward.

Return an empty list when the tree is empty.

Function

largestValues(root: TreeNode) → List<Integer>

Examples

Example 1

root = [1,3,2,5,3,null,9]return = [1,3,9]

The maximum values at depths zero, one, and two are 1, 3, and 9.

Example 2

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

The root is the only value at depth zero, and 3 is the larger child at depth one.

Example 3

root = []return = []

An empty tree has no rows.

Constraints

  • The tree contains between 0 and 10000 nodes.
  • -2147483648 <= node.val <= 2147483647.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public List<Integer> largestValues(TreeNode root) {
    // Write your code here.
}
root[1,3,2,5,3,null,9]
expected[1,3,9]
Checking account…