Floor is the largest element . Ceiling is the smallest element .
def floor(root, x):
result = null
while root:
if root.val == x:
return root.val
if root.val < x:
result = root.val
root = root.right
else:
root = root.left
return result
def ceiling(root, x):
result = null
while root:
if root.val == x:
return root.val
if root.val > x:
result = root.val
root = root.left
else:
root = root.right
return result
Time: . These operations are common in BST-based maps for range queries.