332. Reconstruct Itinerary
Hard NeetCode 150Quick review
- Pattern
- Advanced Graphs
- Time
- O(E log E)
- Space
- O(E)
Standard Solution (C++)
SummaryGiven a list of airline tickets [from, to], reconstruct the single itinerary that uses every ticket exactly once, starting at JFK and, among valid itineraries, choosing the lexicographically smallest.
This is an Eulerian path problem. Build an adjacency map from each airport to a sorted multiset of destinations, then run Hierholzer's algorithm: repeatedly walk to the smallest available next airport, and when a node has no outgoing tickets left, push it onto the route (post-order). Reversing the post-order gives the itinerary. Using a min-ordered multiset guarantees the lexicographically smallest valid path.
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
unordered_map<string, multiset<string>> adj;
for (auto& t : tickets) adj[t[0]].insert(t[1]);
vector<string> route;
stack<string> st;
st.push("JFK");
while (!st.empty()) {
string u = st.top();
auto& dests = adj[u];
if (dests.empty()) {
route.push_back(u);
st.pop();
} else {
string v = *dests.begin();
dests.erase(dests.begin());
st.push(v);
}
}
reverse(route.begin(), route.end());
return route;
}
}; A canonical reference solution. Problem statement © LeetCode — see the official link above.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.