Skip to content
Problem 332 · LeetCode 332(opens in a new tab)

Reconstruct Itinerary

HardDepth-first search

You get a pile of one-way plane tickets. tickets[i] = [from, to] lets a traveller fly once from airport from to airport to. Every airport is a three-letter code in capitals.

The traveller starts at "JFK" and must fly every ticket exactly once, in some order where each flight leaves from the airport the last one landed at. Return the airports visited, in order. If several orders work, return the one that comes first alphabetically when you compare the lists airport by airport. At least one order always works.

Example 1

Input:  tickets = [["JFK","BOS"], ["JFK","DEN"], ["DEN","JFK"]]
Output: ["JFK", "DEN", "JFK", "BOS"]
Why:    going to BOS first strands the DEN → JFK ticket
1 more example and the constraints

Example 2

Input:  tickets = [["LAX","SEA"], ["JFK","ORD"], ["ORD","LAX"]]
Output: ["JFK", "ORD", "LAX", "SEA"]
Why:    only one way to chain the three tickets from JFK

Constraints

  • 1 ≤ number of tickets ≤ 300
  • Each airport code is 3 upper-case letters
  • A ticket never goes from an airport to itself
  • The tickets always fit together into at least one full route from JFK

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Backtracking, smallest firstO(E^E) worst
  2. Eulerian path (Hierholzer)O(E log E)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 300 tickets, worst caseVerdict
Backtracking, smallest firstO(E^E) worst
Works on friendly inputs; no good bound
Eulerian path (Hierholzer)O(E log E)
Best; one walk, no undoing

E = number of tickets.

Edge cases to test

  • A dead end on the smallest choice. Example 1: BOS is smaller than DEN but has no way back, so it must be the last stop.
  • Duplicate tickets. Two copies of JFK → ATL are two separate flights; both must be used.
  • Only one ticket. [["JFK","ATL"]] gives ["JFK", "ATL"].
  • Ending back at JFK. A round trip JFK → ORD → JFK is fine; JFK appears twice in the route.

Common mistakes

  • Greedily taking the smallest ticket and never backing out, which strands tickets at a dead end.
  • Forgetting to reverse the route that Hierholzer builds from the end.
  • Storing destinations in a set, which merges duplicate tickets into one.

How to talk through it

  1. 1Clarify. Must every ticket be used? Can tickets repeat? Is a valid route guaranteed? What does smallest order mean?
  2. 2Give the backtracking. Try the smallest ticket first and undo on a dead end. Correct, but dead ends can make it blow up.
  3. 3Name the graph problem. Using every edge exactly once is an Eulerian path. Hierholzer finds one in a single walk.
  4. 4Code the stack loop. Fly the smallest ticket while you can; when stuck, pop the airport onto the route. Reverse at the end.
  5. 5Test out loud. Run the dead-end example and a case with duplicate tickets before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?