-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.py
41 lines (37 loc) · 878 Bytes
/
BFS.py
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
37
38
39
40
41
import queue
class TreeNode:
def __init__(self, value=None, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __repr__(self):
return str(self.value)
root = TreeNode(3)
root.left = TreeNode(5)
root.left.left = TreeNode(1)
root.left.right = TreeNode(4)
root.right = TreeNode(2)
root.right.left = TreeNode(6)
print("""
Level 1: 3 \n
/ \ \n
Level 2: 5 2 \n
/ \ / \n
Level 3: 1 4 6 \n
""")
# 3 5 2 1 4 6
# use a stack
def levelOrder(root):
s = ""
q = queue.Queue()
q.put(root)
while not q.empty():
n = q.get()
s += str(n) + " "
if(n != None):
if(n.left != None):
q.put(n.left)
if(n.right != None):
q.put(n.right)
return s
print(levelOrder(root))