-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 23: BST Level-Order Traversal.py
More file actions
39 lines (36 loc) · 983 Bytes
/
Day 23: BST Level-Order Traversal.py
File metadata and controls
39 lines (36 loc) · 983 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
37
38
39
import sys
class Node:
def __init__(self,data):
self.right=self.left=None
self.data = data
class Solution:
def insert(self,root,data):
if root==None:
return Node(data)
else:
if data<=root.data:
cur=self.insert(root.left,data)
root.left=cur
else:
cur=self.insert(root.right,data)
root.right=cur
return root
def levelOrder(self,root):
if root is None:
return
queue = []
queue.append(root)
while(len(queue) > 0):
print(queue[0].data, end=" ")
node = queue.pop(0)
if node.left is not None:
queue.append(node.left)
if node.right is not None:
queue.append(node.right)
T=int(input())
myTree=Solution()
root=None
for i in range(T):
data=int(input())
root=myTree.insert(root,data)
myTree.levelOrder(root)