Part 15 of 15 in Binary Search Trees  

Finding floor of a value in Binary Search Tree by phantom11

Problem statement

Write a function to find the floor a given data in a binary search tree. A binary search tree is a binary tree in which value of all the nodes left of a node are lesser than it and value of all the nodes right of node are greater than it. You may assume that there are no duplicate values in the tree. Floor of a value is the largest value in the tree which is less than equal to the given value. For example in the sequence {3, 6, 7} floor of 6 is 3 whereas floor of 8 is 6.

Input

You need to write a function named "FloorInBST" that will take root of a binary search tree and the data whose floor needs to be found as arguments.

Output

Your function must return an integer value which is the floor of the given value in BST. Make sure that the value you return must be a part of the BST. If the floor of the value does not exist in the tree return -1.

Inorder Traversal of BST produces a sorted sequence


To try out your code



Sign in

Sign up