← DSA
HARD

Reconstruct Itinerary

PracticeNeetCode ↗LeetCode ↗

Given a list of airline tickets as [from, to] pairs, reconstruct the itinerary in order, starting from "JFK", using every ticket exactly once. If multiple valid itineraries exist, return the lexicographically smallest one.

Example

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

Key Pointers

Step-by-Step Analysis

  1. Build an adjacency map from airport to a min-heap of destinations, so the lexically smallest option is always available first.
  2. Start a DFS from "JFK". At each airport, while it still has unused outgoing tickets, pop the smallest destination and recurse into it.
  3. Once an airport has no more tickets left to use (a true dead end, reached only after exhausting every option), append it to the route.
  4. Reverse the accumulated route at the end to get the itinerary in forward order.

Complexity

Time: O(E log E) for the heap operations across all tickets — Space: O(E)

Code

from collections import defaultdict
import heapq

def findItinerary(tickets: list[list[str]]) -> list[str]:
    graph = defaultdict(list)
    for src, dst in tickets:
        heapq.heappush(graph[src], dst)

    route = []

    def visit(airport: str) -> None:
        while graph[airport]:
            next_dest = heapq.heappop(graph[airport])
            visit(next_dest)
        route.append(airport)

    visit("JFK")
    return route[::-1]

Code Explanation

How the solution above works, piece by piece.

  1. Store each airport's destinations in a min-heap so they come out in lexical order, as the problem requires.
  2. visit(airport) keeps using the smallest remaining ticket out of airport; each ticket is popped, so it's used exactly once.
  3. Only when an airport has no tickets left is it appended to route. This post-order trick (Hierholzer's algorithm) handles detours: a dead end reached too early ends up at the end of the final route.
  4. The route is built backwards, so return route[::-1].
  5. O(E log E) for E tickets because of the heaps.

Visual Walkthrough

Running it on the example tickets:

Itinerary JFK MUC LHR SFO SJC with finish orderJFKMUCLHRSFOSJC12345appended to route (badge order): SJC, SFO, LHR, MUC, JFKreversed → JFK → MUC → LHR → SFO → SJC
The DFS always takes the alphabetically smallest unused ticket. An airport is added to route only when it has no tickets left, so the dead end SJC is added first and JFK last (badges). Reversing gives the itinerary.
airport

Video Explanation

NeetCode — Reconstruct Itinerary, LeetCode 332