<p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>

<p>All of the tickets belong to a man who departs from <code>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>

<ul>
	<li>For example, the itinerary <code>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</code>.</li>
</ul>

<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]]
<strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]]
<strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;]
<strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= tickets.length &lt;= 300</code></li>
	<li><code>tickets[i].length == 2</code></li>
	<li><code>from<sub>i</sub>.length == 3</code></li>
	<li><code>to<sub>i</sub>.length == 3</code></li>
	<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
	<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>

class Solution:
    def findItinerary(self, tickets: List[List[str]]) -> List[str]:
        # this question is not your easy DFS question. in here, there's a concept
        # called eulerian circuit/cycle which states that the trail starts and 
        # ends on the same vertex. the algo consists of 2 steps:
        # 1. Starting from any vertex, we keep following the unused edges until 
        # we get stuck at certain vertex where we have no more unvisited outgoing edges.
        # 2. We then backtrack to the nearest neighbor vertex in the current path that 
        # has unused edges and we repeat the process until all the edges have been used.
        # The first vertex that we got stuck at would be the end point of our Eulerian 
        # path. So if we follow all the stuck points backwards, we could reconstruct 
        # the Eulerian path at the end.

        # since, we need return the itinerary that has the smallest lexical order, we have
        # to sort the tickets list so that it would be easy to form the sorted nature in
        # dfs calls, after processing each node, we pop it from the adjList since we can
        # use each ticket only once and call dfs for the nextNode. Save the curr node in the result.
        # [TC: O((V+E)LogN) SC: O(V+E)]
        if not tickets:
            return []
        tickets.sort()
        adjList = defaultdict(list)
        for a, b in tickets:
            adjList[a].append(b)
        result = []
        visited = set()
        def dfs(node):
            for i, nextNode in enumerate(adjList[node]):
                # node -> curr airport, i -> index of destination
                if (node, i) not in visited:
                    visited.add((node, i))
                    dfs(nextNode)
            result.append(node)      

        # def dfs(node):
        #     while adjList[node]:
        #         nextNode = adjList[node].pop(0)
        #         dfs(nextNode)
        #     result.append(node)

        dfs("JFK")
        return result[::-1]
17000