-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloor in BST.py
40 lines (33 loc) · 862 Bytes
/
Floor in BST.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
class Solution:
def floor(self, root, x):
prev = None
while root != None:
if root.data == x:
return x
elif root.data < x:
prev = root
root = root.right
else:
root = root.left
return prev.data if prev is not None else -1
class Node:
def __init__(self, val):
self.right = None
self.data = val
self.left = None
def insert(tree, val):
if tree == None:
return Node(val)
if val < tree.data:
tree.left = insert(tree.left, val)
elif val > tree.data:
tree.right = insert(tree.right, val)
return tree
n = int(input())
arr = list(map(int, input().split()))
root = None
for k in arr:
root = insert(root, k)
s = int(input())
obj = Solution()
print(obj.floor(root, s))