Uber ATG | Onsite | N-ary Tree Reconstruction
Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2019-08-17Added By Anonymous
Source: Link
Company: Uber
Role: unknown
Interview Stage: Onsite
Location: unknown
Year: unknown
Original Post Content:
Given an unordered list of pairs where each pair represents a parent-child relationship (not nessesary direct) in an N-ary tree. Reconstruct and return the original tree. You can assume that each node has a unique value.
Example 1:
Input: [[1, 2], [1, 3], [1, 4], [2, 4]]
Output:
1
/ \
2 3
/
4
Example 2:
Input: [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 4], [2, 5], [2, 7], [3, 6], [4, 7]]
Output:
1
/ \
2 3
/ \ \
4 5 6
/
7