【leetcode】332. Reconstruct Itinerary
程序员文章站
2022-03-07 17:25:49
...
欧拉通路
提交代码
class Solution {
Map<String, PriorityQueue<String>> graph =new HashMap<>();
List<String> path=new LinkedList<>();
public List<String> findItinerary(List<List<String>> tickets) {
for(List<String> t: tickets)
// graph.getOrDefault(t.get(0), new PriorityQueue<String>()).offer(t.get(1));
graph.computeIfAbsent(t.get(0), k->new PriorityQueue<String>()).add(t.get(1));
visit("JFK");
return path;
}
private void visit(String airport) {
while(graph.containsKey(airport)&&graph.get(airport).size()>0)
visit(graph.get(airport).poll());
path.add(0, airport);
}
}