{"id":252,"date":"2017-09-12T19:34:04","date_gmt":"2017-09-13T02:34:04","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=252"},"modified":"2018-07-10T18:50:48","modified_gmt":"2018-07-11T01:50:48","slug":"leetcode-332-reconstruct-itinerary","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-332-reconstruct-itinerary\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 332. Reconstruct Itinerary"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 332. Reconstruct Itinerary - \u5237\u9898\u627e\u5de5\u4f5c EP52\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/4udFSOWQpdg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem<\/strong>:<\/p>\n<p>Given a list of airline tickets represented by pairs of departure and arrival airports\u00a0<code>[from, to]<\/code>, reconstruct the itinerary in order. All of the tickets belong to a man who departs from\u00a0<code>JFK<\/code>. Thus, the itinerary must begin with\u00a0<code>JFK<\/code>.<\/p>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary\u00a0<code>[\"JFK\", \"LGA\"]<\/code>\u00a0has a smaller lexical order than\u00a0<code>[\"JFK\", \"LGB\"]<\/code>.<\/li>\n<li>All airports are represented by three capital letters (IATA code).<\/li>\n<li>You may assume all tickets form at least one valid itinerary.<\/li>\n<\/ol>\n<p><b>Example 1:<\/b><br \/>\n<code>tickets<\/code>\u00a0=\u00a0<code>[[\"MUC\", \"LHR\"], [\"JFK\", \"MUC\"], [\"SFO\", \"SJC\"], [\"LHR\", \"SFO\"]]<\/code><br \/>\nReturn\u00a0<code>[\"JFK\", \"MUC\", \"LHR\", \"SFO\", \"SJC\"]<\/code>.<\/p>\n<p><b>Example 2:<\/b><br \/>\n<code>tickets<\/code>\u00a0=\u00a0<code>[[\"JFK\",\"SFO\"],[\"JFK\",\"ATL\"],[\"SFO\",\"ATL\"],[\"ATL\",\"JFK\"],[\"ATL\",\"SFO\"]]<\/code><br \/>\nReturn\u00a0<code>[\"JFK\",\"ATL\",\"JFK\",\"SFO\",\"ATL\",\"SFO\"]<\/code>.<br \/>\nAnother possible reconstruction is\u00a0<code>[\"JFK\",\"SFO\",\"ATL\",\"JFK\",\"ATL\",\"SFO\"]<\/code>. But it is larger in lexical order.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Idea<\/strong>:<\/p>\n<p>Convert the graph to a tree and do post-order traversal<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-257\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-256\" style=\"font-size: 1rem;\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-255\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-3.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-3-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/332-ep52-3-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>\u00a0Solution:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true  \">\/\/ Author: Huahua\r\n\/\/ Running time: 13 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;string&gt; findItinerary(vector&lt;pair&lt;string, string&gt;&gt; tickets) {\r\n        route_.clear();\r\n        trips_.clear();\r\n        \r\n        for(const auto&amp; pair : tickets)\r\n            trips_[pair.first].push_back(pair.second);\r\n        \r\n        for(auto&amp; pair : trips_) {\r\n            auto&amp; dests = pair.second;\r\n            std::sort(dests.begin(), dests.end());\r\n        }\r\n        \r\n        const string kStart = \"JFK\";\r\n        \r\n        visit(kStart);\r\n        \r\n        return vector&lt;string&gt;(route_.crbegin(), route_.crend());\r\n    }\r\nprivate:\r\n    \/\/ src -&gt; {dst1, dest2, ..., destn}\r\n    unordered_map&lt;string, deque&lt;string&gt;&gt; trips_;    \r\n    \/\/ ans (reversed)\r\n    vector&lt;string&gt; route_;\r\n    \r\n    void visit(const string&amp; src) {\r\n        auto&amp; dests = trips_[src];\r\n        while (!dests.empty()) {\r\n            \/\/ Get the smallest dest\r\n            const string dest = dests.front();\r\n            \/\/ Remove the ticket\r\n            dests.pop_front();\r\n            \/\/ Visit dest\r\n            visit(dest);\r\n        }\r\n        \/\/ Add current node to the route\r\n        route_.push_back(src);\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a list of airline tickets represented by pairs of departure and arrival airports\u00a0[from, to], reconstruct the itinerary in order. All of the tickets&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[77,177,56,28],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-medium","tag-postorder","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":3074,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions\/3074"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}