class Solution { public: vector<string> path; unordered_map<string, multiset<string>> m; vector<string> findItinerary(vector<pair<string, string>> tickets) { for (auto &p : tickets) m[p.first].insert(p.second); dfs("JFK"); reverse(path.begin(), path.end()); return path; } void dfs(const string cur) { while (!m[cur].empty()) { auto peer = m[cur].begin(); string next = *peer; m[cur].erase(peer); dfs(next); } path.push_back(cur); } };