Problem · Graph
Reconstruct Itinerary
Learn this problemProblem statement
You are given airline tickets represented as [from, to] pairs. Reconstruct the itinerary that uses every ticket exactly once.
The itinerary must start at "JFK". If multiple valid itineraries exist, return the lexicographically smallest itinerary when read as a single sequence of airport codes.
Function
findItinerary(tickets: String[][]) → String[]Examples
Example 1
tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]return = ["JFK","MUC","LHR","SFO","SJC"]There is only one route that uses all tickets from JFK.
Example 2
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]return = ["JFK","ATL","JFK","SFO","ATL","SFO"]Both choices from JFK can lead to valid routes, but the route beginning with ATL is lexicographically smaller.