Traverse an Up-Down-Right Pointer Structure
Problem statement
Node i has value values[i] and up, down, and right target indices or -1. Starting at start, follow any of those directed pointers. Return every reachable node value once in ascending order.
Function
accessibleValues(values: int[], up: int[], down: int[], right: int[], start: int) → int[]Examples
Example 1
values = [1,2,3,4,5,6,7,8,9,10,11,12]up = [-1,0,1,-1,-1,-1,-1,6,-1,-1,9,-1]down = [-1,-1,3,-1,5,-1,-1,8,-1,-1,-1,-1]right = [-1,-1,4,-1,7,-1,-1,10,-1,-1,11,-1]start = 2return = [1,2,3,4,5,6,7,8,9,10,11,12]Starting at value 3 reaches every displayed node.
Constraints
- All arrays have equal length.
- The reachable pointer graph is acyclic.