Back to problems

Validate BFS Order on a Tree Built from Two Parent Vectors

Algorithm · Rubrik · Hard

Two arrays, vec1 and vec2, both with m elements, describe an undirected graph: each index i corresponds to an undirected edge connecting vec1[i] and vec2[i]. There is also a hierarchy rule: inside either array, a node that appears earlier is higher in the tree (closer to the root), and a node that appears later is lower. Under this condition, the edges form a tree on n nodes, labeled by integers. You are given q queries. Each query is an array order of length n; it…

Checking your access…