Imagine you had a perfect sequence of unique numbers, but somehow it got scrambled! ๐ฑ All you have left are clues about which numbers were neighbors in the original array.
You're given a 2D array adjacentPairs where each adjacentPairs[i] = [u, v] tells you that numbers u and v were sitting right next to each other in the original array. Your mission? Restore the original sequence!
๐ The Challenge: The pairs can be given in any order, and each pair [u, v] could represent either u followed by v OR v followed by u in the original array.
Example: If adjacentPairs = [[2,1],[3,4],[3,2]], the original array could be [1,2,3,4] because:
โข 1 and 2 are adjacent
โข 2 and 3 are adjacent
โข 3 and 4 are adjacent
Think of it like solving a jigsaw puzzle where you only know which pieces touch each other!
Input & Output
Constraints
- nums.length == n
- adjacentPairs.length == n - 1
- adjacentPairs[i].length == 2
- -105 โค adjacentPairs[i][0], adjacentPairs[i][1] โค 105
- There exists some nums that satisfies adjacentPairs
- All elements in nums are unique