没能做出来,参考别人的:
1 from heapq import heapify, heappush, heappop 2 3 class Solution: 4 def findItinerary(self, tickets: List[List[str]]) -> List[str]: 5 graph = collections.defaultdict(list) 6 for src, des in tickets: 7 heappush(graph[src], des) 8 res = [] 9 self.DFS('JFK', graph, res) 10 return res[::-1] 11 12 def DFS(self, src, graph, res): 13 while graph[src]: 14 self.DFS(heappop(graph[src]), graph, res) 15 res += [src] 16 return
算法思路:深度优先遍历DFS + 堆。在中等难度的题目中,这道题目属于比较困难的了。