Skip to content

Commit ca322da

Browse files
committed
Add Euler's path solution
1 parent 67b524f commit ca322da

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import defaultdict
2+
from heapq import heappop, heappush
3+
4+
class Solution:
5+
def euler_path(self, current, path, edges):
6+
while edges[current]:
7+
next_vertex = heappop(edges[current])
8+
self.euler_path(next_vertex, path, edges)
9+
10+
path.append(current)
11+
return reversed(path)
12+
13+
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
14+
edges = defaultdict(list)
15+
for start, end in tickets:
16+
heappush(edges[start], end)
17+
18+
path = []
19+
return self.euler_path("JFK", path, edges)

0 commit comments

Comments
 (0)