forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.py
More file actions
36 lines (25 loc) · 601 Bytes
/
bfs.py
File metadata and controls
36 lines (25 loc) · 601 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import collections
class Node():
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def bfs(root):
if not root:
return
queue = collections.deque([root])
while queue:
temp = queue.popleft()
print(temp.val)
if temp.right:
queue.append(temp.right)
if temp.left:
queue.append(temp.left)
root = Node(3)
root.right = Node(4)
root.left = Node(2)
root.left.left = Node(1)
root.left.right = Node(2.5)
root.right.left = Node(3.5)
root.right.right = Node(5)
bfs(root)