FastPrepN-ary Tree Right Side View

N-ary Tree Right Side View

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Node i has value values[i] and ordered child indices children[i]; root is 0. Return the value of the rightmost node on every level from top to bottom.

Function

naryRightSideView(values: int[], children: int[][]) → int[]

Examples

Example 1

values = [1,2,3,4,5]children = [[1,2,3],[4],[],[],[]]return = [1,4,5]

Indices 0, 3, and 4 are rightmost on their levels.

Constraints

  • Children form one rooted tree when values is nonempty.
  • At most 10^5 nodes.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] naryRightSideView(int[] values, int[][] children) {
  // Write your code here.
}
values[1,2,3,4,5]
children[[1,2,3],[4],[],[],[]]
expected[1,4,5]
Checking account…