class Solution { public: vector<string> findItinerary(vector<pair<string, string>> tickets) { vector<string> res; stack<string> st{{"JFK"}}; unordered_map<string, multiset<string>> m; for (auto t : tickets) { m[t.first].insert(t.second); } while (!st.empty()) { string t = st.top(); if (m[t].empty()) { res.insert(res.begin(), t); st.pop(); } else { st.push(*m[t].begin()); m[t].erase(m[t].begin()); } } return res; } };

Solution

It is an example of Eulerian path finding. The first solution is just a regular DFS. It tries every possible way and save the current result. However, it obviously records more information it needs. The reason it needs to reset failed path is that it meets a dead end but not all edges are visited. But, there must exists an Eulerian path. Thus, if we meet a dead end, it tells us that we just need to visit other points and there must be a way to go back to the start point of the dead end path. As a result, we can record the dead end path and output them at last. It is the method of Hierholzer’s algorithm. The second solution is the Hierholzer’s algorithm.

Valuable Method

It is not hard to write the first method and also not hard to think about the second method even we don’t know Hierholzer’s algorithm before. If you know the Hierholzer’s algorithm, you can realize it immediately and solve the problem. But if not, just dig more from the Eulerian path rather than submitting a passable solution and you will learn more from the problem.