Reconstruct Itinerary - Problem

Imagine you're a travel enthusiast who just returned from an amazing multi-city trip! You have a pile of airline tickets, but they're all mixed up. Your goal is to reconstruct your complete travel itinerary in the correct order.

You are given a list of airline tickets where tickets[i] = [from_i, to_i] represents the departure and arrival airports of one flight. All tickets belong to someone who started their journey from "JFK" airport.

Requirements:

  • The itinerary must begin with "JFK"
  • You must use all tickets exactly once
  • If multiple valid itineraries exist, return the one with the smallest lexicographical order
  • For example: ["JFK", "LGA"] comes before ["JFK", "LGB"]

Think of this as finding an Eulerian path in a directed graph where each ticket represents an edge!

Input & Output

example_1.py β€” Basic Case
$ Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
β€Ί Output: ["JFK","MUC","LHR","SFO","SJC"]
πŸ’‘ Note: Starting from JFK, we can only go to MUC. From MUC to LHR, LHR to SFO, and finally SFO to SJC. This uses all tickets exactly once.
example_2.py β€” Multiple Valid Paths
$ Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
β€Ί Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
πŸ’‘ Note: Multiple valid itineraries exist, but we choose the lexicographically smallest: JFKβ†’ATL comes before JFKβ†’SFO, so we start with ATL path.
example_3.py β€” Circular Route
$ Input: tickets = [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]]
β€Ί Output: ["JFK","NRT","JFK","KUL"]
πŸ’‘ Note: We have two choices from JFK: KUL or NRT. Since we need to use all tickets, we must choose the path that allows us to return and use the remaining ticket.

Visualization

Tap to expand
Flight Network: Graph RepresentationJFKATLSFOLAXMIATicket 1Ticket 2Ticket 3Ticket 4Ticket 5🎯 Key Insights:β€’ This is an Eulerian Path problem - traverse every edge exactly onceβ€’ Use Hierholzer's Algorithm: DFS + remove edges + post-order recordingβ€’ Sort destinations alphabetically to ensure lexicographical orderβ€’ Backtrack when stuck and record nodes in reverse order⚑ Time: O(E log E) | Space: O(E)
Understanding the Visualization
1
Model as Graph
Each airport is a node, each ticket is a directed edge
2
Sort Destinations
For each airport, sort its destinations alphabetically
3
DFS with Edge Removal
Start from JFK, follow edges and remove them as we use tickets
4
Backtrack Strategy
When stuck, backtrack and record the current airport
5
Reverse Path
The recorded path is in reverse order, so flip it for the final answer
Key Takeaway
🎯 Key Insight: This is fundamentally an Eulerian path problem. By using Hierholzer's algorithm with DFS and careful edge removal, we can efficiently find the lexicographically smallest valid itinerary in O(E log E) time.

Time & Space Complexity

Time Complexity
⏱️
O(E log E)

E is number of tickets. Sorting destinations takes O(E log E), DFS visits each edge once

n
2n
⚑ Linearithmic
Space Complexity
O(E)

Space for adjacency list, recursion stack, and result array

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ tickets.length ≀ 300
  • tickets[i].length == 2
  • fromi.length == 3
  • toi.length == 3
  • fromi and toi consist of uppercase English letters
  • All tickets belong to a man who departs from "JFK"
  • All tickets form at least one valid itinerary
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28 Apple 15
89.4K Views
Medium-High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen