Populate Next Right Pointers
Problem statement
Node indices are 0 through n-1; root is 0. left[i] and right[i] are child indices or -1.
Return an array where result[i] is the next node to the right of i on the same level, or -1 for the level's final node.
Function
populateNextRight(left: int[], right: int[]) → int[]Examples
Example 1
left = [1,3,-1,-1,-1,-1,-1]right = [2,4,5,-1,-1,6,-1]return = [-1,2,-1,4,5,-1,-1]Each level is linked left-to-right by node index.
Constraints
- Arrays have equal length and encode one binary tree.
- At most
10^5nodes.