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
Visualization
Time & Space Complexity
E is number of tickets. Sorting destinations takes O(E log E), DFS visits each edge once
Space for adjacency list, recursion stack, and result array
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