You are given a pairs, where pairs[i] = [x_i, y_i] indicates that there is an ancestor or descendant relationship between nodes x_i and y_i in a rooted tree. That is, either x_i is an ancestor of y_i, or y_i is an ancestor of x_i.
A rooted tree is valid if:
It contains all nodes that appear in pairs.
For every pair [x_i, y_i] in pairs, either x_i is an ancestor of y_i or y_i is an ancestor of x_i.
For any two nodes not in pairs, neither is an ancestor of the other.
Return:
Note: A node
uis an ancestor ofvifulies on the path from the root tovandu \neq v.
Constraints:
pairs.length
x_i y_i
The elements in pairs are unique.
You are given a pairs, where pairs[i] = [x_i, y_i] indicates that there is an ancestor or descendant relationship between nodes x_i and y_i in a rooted tree. That is, either x_i is an ancestor of y_i, or y_i is an ancestor of x_i.
A rooted tree is valid if:
It contains all nodes that appear in pairs.
For every pair [x_i, y_i] in pairs, either x_i is an ancestor of y_i or y_i is an ancestor of x_i.
For any two nodes not in pairs, neither is an ancestor of the other.
Return:
Note: A node
uis an ancestor ofvifulies on the path from the root tovandu \neq v.
Constraints:
pairs.length
x_i y_i
The elements in pairs are unique.