Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- class Solution:
- def bfs(self, adj):
- visited={}
- ans=[]
- q=deque([])
- q.append(0)
- visited[0]=True
- while len(q)>0:
- k=len(q)
- for i in range(k):
- temp=q.popleft()
- ans.append(temp)
- for k in adj[temp]:
- if visited.get(k,False)==False:
- visited[k]=True
- q.append(k)
- return ans
Advertisement
Add Comment
Please, Sign In to add comment