You are given a list of airline tickets where tickets[i] = [fromi, toi] 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.
["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 == 2fromi.length == 3toi.length == 3fromi and toi consist of uppercase English letters.fromi != toiProblem Overview: You receive a list of airline tickets where each ticket represents a directed edge [from, to]. Starting from "JFK", reconstruct the full travel itinerary that uses every ticket exactly once. If multiple valid routes exist, return the itinerary with the smallest lexical order.
The key observation: every ticket must be used exactly once, which turns the problem into finding an Eulerian path in a directed graph. Airports are nodes, tickets are edges. The itinerary is the path that visits every edge exactly once while respecting lexical ordering.
Approach 1: Hierholzer's Algorithm for Eulerian Path (O(E log E) time, O(E) space)
Model the tickets as a directed graph using an adjacency list. Because the itinerary must be lexicographically smallest, store each airport's destinations in sorted order (often implemented with a min-heap or reversed sorted list). Then run a depth-first traversal based on Hierholzer's algorithm. The DFS always takes the smallest available destination and removes that edge from the graph. When a node has no remaining outgoing edges, append it to the route. This naturally builds the itinerary in reverse order, so the final answer is reversed at the end. The algorithm works because Eulerian paths are constructed by exploring edges until a dead end, then backtracking while stitching partial circuits together. This is the most common interview solution and directly leverages concepts from graph traversal and Depth-First Search. Time complexity is O(E log E) due to sorting or heap operations, and space complexity is O(E) for the adjacency structure and recursion stack.
Approach 2: Iterative Stack-based Simulation (O(E log E) time, O(E) space)
This approach implements the same Eulerian path construction but avoids recursion. Instead of recursive DFS, maintain an explicit stack starting with "JFK". While the airport at the top of the stack still has outgoing flights, push the smallest destination onto the stack and remove that edge from the adjacency list. When an airport has no remaining outgoing edges, pop it from the stack and append it to the itinerary. This mimics the postorder behavior of DFS and constructs the path in reverse order, just like Hierholzer's algorithm. The iterative approach is useful in environments where recursion depth might be limited and gives more explicit control over the traversal state. The complexity remains O(E log E) time and O(E) space because edge ordering is still required.
Recommended for interviews: The Hierholzer DFS approach is what most interviewers expect because the problem is essentially an Eulerian path construction with lexical ordering. Showing that you recognize the graph structure and apply Hierholzer's algorithm demonstrates strong algorithmic understanding. The iterative stack version is a good follow-up optimization that shows you understand how DFS traversal works internally.
Approach Description: Hierholzer's algorithm helps find the Eulerian path in a graph. The key idea is to keep backtracking and viewing if there's another unexplored path. We start from the node 'JFK' and explore possible destinations using DFS while following lexical order to ensure the smallest lexical itinerary.
This solution creates a graph where each key is a departure point and the value is a list of destinations sorted in lexical order.
We use a stack to track the journey from 'JFK'. For each node, we explore its destinations until we exhaust all paths, appending each node to our result list in the order we backtrack. Finally, we reverse the result path to get the correct itinerary.
Time Complexity: O(N log N) where N is the number of tickets, due to sorting.
Space Complexity: O(N) to store the graph and the route.
Approach Description: Another way to simulate the backtracking approach is using an iterative stack to push/pop airports while ensuring that we appropriately capture our path by focusing on edge traversal.
Using a map of priority queues, this C++ code builds a graph of tickets. The stack represents the ongoing traversal through JFK and beyond, continually adding destinations in lexically sorted order. The itinerary is completed by backtracking through available flights, culminating in a reversed route list.
C++
JavaScript
Time Complexity: O(N log N) due to priority queue operations.
Space Complexity: O(N) for storing the graph and itinerary.
The problem is essentially about finding a path that starts from a specified starting point, passes through all the edges exactly once, and has the smallest lexicographical order among all such paths, given n vertices and m edges. This is a classic Eulerian path problem.
Since the problem guarantees that there is at least one feasible itinerary, we can directly use the Hierholzer algorithm to output the Eulerian path starting from the starting point.
The time complexity is O(m times log m), and the space complexity is O(m). Here, m is the number of edges.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Hierholzer's Algorithm for Eulerian Path | Time Complexity: O(N log N) where N is the number of tickets, due to sorting. |
| Approach 2: Iterative Stack-based Simulation | Time Complexity: O(N log N) due to priority queue operations. |
| Eulerian Path | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hierholzer's Algorithm with DFS | O(E log E) | O(E) | Best general solution. Clean implementation and commonly expected in interviews. |
| Iterative Stack-based Simulation | O(E log E) | O(E) | Useful when avoiding recursion or when implementing DFS traversal explicitly. |
Reconstruct Itinerary - Leetcode 332 - Python • NeetCode • 115,139 views views
Watch 9 more video solutions →Practice Reconstruct Itinerary with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor