Skip to content
AI360Xpert
Beta
Difficulty: HardAdvanced Graphs

Reconstruct Itinerary

Problem in Plain English

Given every flight ticket as a directed edge, rebuild the complete trip that starts at 'JFK' and uses each ticket exactly once, choosing the smallest lexical order when several trips work.

Problem Statement

You are given a list of airline tickets where each tickets[i] = [from, to] is one directed flight from airport from to airport to. Reconstruct the full itinerary that starts at airport JFK and uses every ticket exactly once, returning the airports in visiting order.

When several itineraries consume all tickets, return the one that is smallest in lexical order when read as a single string. The input always admits at least one valid itinerary, and duplicate tickets between the same airports may appear.

Constraints

  • 1 <= tickets.length <= 300
  • tickets[i].length == 2
  • Each airport code has length 3 and uses uppercase English letters.
  • Departure and arrival airports of one ticket are never equal.

Examples

Example 1

Input
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output
["JFK","ATL","JFK","SFO","ATL","SFO"]

The route ["JFK","SFO","ATL","JFK","ATL","SFO"] also uses every ticket but is larger in lexical order.

Example 2

Input
tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
Output
["JFK","MUC","LHR","SFO","SJC"]

Only one ordering consumes every ticket: JFK to MUC to LHR to SFO to SJC.

Example 3

Input
tickets = [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]]
Output
["JFK","NRT","JFK","KUL"]

KUL is smaller than NRT but has no departure, so taking it first strands two tickets. The NRT cycle must come before the final JFK to KUL flight.

Example 4

Input
tickets = [["JFK","ATL"]]
Output
["JFK","ATL"]

One ticket needs two airport entries: the starting JFK and its only destination ATL.

Example 5

Input
tickets = [["JFK","ATL"],["JFK","ATL"],["ATL","JFK"]]
Output
["JFK","ATL","JFK","ATL"]

The two identical JFK to ATL tickets are separate flights. Returning to JFK between them consumes both copies exactly once.

Intuition

Tickets are directed edges, and repeated airport names are allowed because the restriction is on tickets, not airports. Example 1 must use both departures from JFK and both from ATL. A small next airport is desirable only if the remaining tickets can still be used: with JFK to KUL, JFK to NRT and NRT to JFK, choosing KUL immediately gets stuck. Backtracking tests whether a choice can finish; postorder traversal instead delays each stuck airport until the end of the final itinerary.

Approaches

DFS Backtracking Search

Solution Details

Reveal DFS Backtracking Search: intuition, complexity, and code

Hints

Hint 1
Using every ticket once means the route is an Eulerian trail through the flight graph, not a shortest path.
Hint 2
Explore the smallest unused departure first, keeping each airport departures reverse-sorted so pop takes it in O(1)O(1) time; exploration order alone is not the final route.
Hint 3
Append an airport to the answer after its departures run out, then reverse the answer, because the stuck airport is the trip end.

Edge Cases

  • Duplicate tickets between the same airports stay correct because each copy is its own stack entry popped exactly once.
  • Cycles such as ATL to JFK back to ATL resolve naturally since the unwind order places the cycle interior before its entry airport.
  • Airports with no departures, like the final stop, only ever appear as appends during the unwind.

Common Mistakes and Interview Tips

  • Greedily locking in the smallest next airport without backtracking strands tickets and returns a route shorter than tickets.length + 1.
  • Sorting departures ascending and removing from the front repeats linear shifts; reverse-sort and pop from the end instead.
  • Appending airports on the way down instead of on the unwind keeps lexical order but drops tickets trapped inside cycles.

Key Takeaway

When a graph problem demands using every edge exactly once, think Eulerian trail and build the answer with postorder appends: consume edges on the way down and record vertices on the way back up.