Reconstruct Itinerary Problem
Description
LeetCode Problem 332.
You are given a list of airline tickets where tickets[i] = [from_i, to_i] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from "JFK", thus, the itinerary must begin with "JFK". If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
- For example, the itinerary
["JFK", "LGA"]has a smaller lexical order than["JFK", "LGB"].
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
Example 1:

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

Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
Constraints:
1 <= tickets.length <= 300tickets[i].length == 2from_i.length == 3to_i.length == 3from_iandto_iconsist of uppercase English letters.from_i != to_i
Solution
Approach: Hierholzer's Algorithm
Think of airports as nodes and tickets as directed edges. Using every ticket exactly once is walking an Eulerian path that starts at "JFK", and we want the lexicographically smallest one.
A simple greedy walk ("always take the smallest unused ticket") almost works, but it can get stuck. In [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]], going to KUL first is a dead end with tickets left over; the right answer is JFK, NRT, JFK, KUL.
Hierholzer's algorithm fixes this. We still always take the smallest unused ticket, but we write an airport into the route only when we get stuck there, and the route comes out in reverse:
- Sort each airport's destinations so the smallest is used first (we store them in reverse order and pop from the end).
- Keep a stack, starting with
"JFK". If the airport on top still has unused tickets, pop its smallest destination and push it. Otherwise, pop the airport and append it toroute. - At the end, reverse
route.
Why it works: the first airport we get stuck at must be the final destination of the whole trip (in the example, KUL), so it is correct to put it last. The remaining tickets form loops that come back to airports on the stack, and those loops are added before it. Taking the smallest ticket first at each step still gives the smallest order among the valid itineraries.
The stack version avoids recursion, though a recursive DFS that appends an airport after visiting all its tickets is the same algorithm.
Complexity: O(E log E) time for sorting the E tickets, and O(E) extra space.
from collections import defaultdict
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
graph = defaultdict(list)
for src, dst in sorted(tickets, reverse=True):
graph[src].append(dst) # reverse order, so pop() returns the smallest destination
route = []
stack = ["JFK"]
while stack:
if graph[stack[-1]]:
stack.append(graph[stack[-1]].pop()) # fly the smallest unused ticket
else:
route.append(stack.pop()) # stuck here: this airport is final among what is left
return route[::-1]