LeetCode 332. Reconstruct Itinerary
本题是有向图找 Euler Path 的问题。可以用 Hierholzer’s Algorithm for the directed graph https://www.geeksforgeeks.org/hierholzers-algorithm-directed-graph/ Hierholzer’s Algorithm 的本质和树里的后序遍历很像。对于 Euler Circuit 来说,dfs如果到头了,说明当前这条路径已经构成了一条回路,backtrack回去把别的道路的 circuit 加到当前 circuit 即可。 由于是dfs,所以可以有 Recursive 和 Iterative 两种写法。 Recursive: https://leetcode.com/problems/reconstruct-itinerary/discuss/78835/28ms-C++-beats-100-Short-and-Elegant. Iterative: https://leetcode.com/problems/reconstruct-itinerary/discuss/78842/C++-non-recursive-O(N)-time-O(N)-space-solution-with-detail-explanations 以下代码是 Recursive class