Skip to content

Commit

Permalink
39
Browse files Browse the repository at this point in the history
  • Loading branch information
park-jiyeong committed Aug 7, 2024
1 parent 97508e0 commit 5892cea
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions JYPARK/31to40/39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from collections import defaultdict, deque

def solution(graph, start):
tree = defaultdict(list)
visited = set()
answer = []
for i, j in graph:
tree[i].append(j)
return bfs(tree, start, visited, answer)

def bfs(tree, node, visited, answer):
q = deque()
q.append(node)

while q:
node = q.popleft()
if node not in visited:
visited.add(node)
answer.append(node)
q.extend(tree[node])
return answer

#graph = [(1, 2), (1, 3), (2, 4), (2, 5),(3, 6),(3, 7),(4, 8), (5, 8),(6, 9),(7, 9)]
graph = [(0,1), (1, 2), (2, 3), (3, 4),(4, 5),(5, 0)]
start = 1
print(solution(graph, start))

0 comments on commit 5892cea

Please sign in to comment.