You are given a hierarchy of n students represented by a parent array par of length n.
1..n
.
par[i]
is the parent (mentor/manager) of student
i+1
.
par[root-1] = -1
.
par[i]
is an integer in
[1..n]
.
A valid lineup is an ordering of all students such that each student appears after their parent.
Task: Return the lexicographically smallest valid lineup (compare two lineups by the first position where they differ; the one with the smaller student id is smaller).
Example
Input:
par = [-1, 1, 1, 2, 2, 2, 2]
This means:
One correct output:
[1, 2, 3, 4, 5, 6, 7]
Constraints (assume): 1 <= n <= 2e5.