forked from srbcheema1/Algo_Ds
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ceilBST
29 lines (26 loc) · 795 Bytes
/
ceilBST
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
"""
Following is the TreeNode class structure
class TreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
"""
import sys
def findCeil(root, x):
# Write your code here.
ceil = sys.maxsize
# assuming between inf and x
while root:
if root.data > x:
if root.data < ceil: # we found a closer ceil to x
ceil = root.data
root = root.left # to find a closer ceil
elif root.data < x: # even for equal need to find a value greater than x
root = root.right # to find a number greater than x
elif root.data == x:
ceil = root.data
break
if ceil != sys.maxsize:
return ceil
return -1