풀이

import collections 
class Solution(object): 
    def findItinerary(self, tickets): 
        """ 
        :type tickets: List[List[str]] 
        :rtype: List[str] 
        """ 
        graph = collections.defaultdict(list) 
        for a, b in sorted(tickets, reverse=True): 
            graph[a].append(b) 

        route = [] 
        def dfs(a): 
            while graph[a]: 
                dfs(graph[a].pop()) 
            route.append(a) 

        dfs('JFK') 

        return route[::-1]
복사했습니다!