Floor in BST
Floor in BST is a GFG problem (Medium). Given the root of a binary search tree and a number x, return the floor of x: the largest value in the tree that is less than or equal to x.
- If
xitself is in the tree, the floor isx. - If every value in the tree is larger than
x, return -1.
The expected solution runs in O(height) time with O(1) extra space, so it may look at only one node per level.
- The tree is a binary search tree with at least one node and distinct values.
- Return -1 if no value in the tree is ≤ x.
- Expected: O(height) time, O(1) extra space.
Intuition
Walk down from the root as if searching for x. Each comparison rules out one whole side of the tree:
- Node larger than
x: it cannot be the floor, and its right subtree holds even larger values. Only the left side can help. - Node smaller than
x: it is a possible floor. Its left subtree holds only smaller values, so nothing there can beat it, but its right subtree may hold a value closer tox. Save it, then go right.
Every saved value is larger than the one before it, because each later save comes from a right subtree. So when the walk ends, the last saved value is the floor.
When a BST question asks for the value closest to x on one side, walk a single path from the root and keep the best answer seen so far. The same skeleton gives the ceil, the inorder predecessor and the inorder successor, with only the comparison or the turn changed.
Approach
Before reading on: take the tree [8,3,10,1,6,null,14,null,null,4,7,13] and x = 5. At the root 8, which side can hold the floor? At node 3, why is 3 not yet the final answer? Aim for O(height) time and O(1) space.
Start with no answer
Set ans = -1 and point a cursor at the root. If the walk never meets a value ≤ x, this -1 is already the required output.
Return at once on an exact match
If the node's value equals x, return x. A value ≤ x can never be larger than x, so nothing anywhere in the tree can beat it.
Save a smaller node and go right
If the value is less than x, set ans to it and move to the right child. The right subtree is the only place a larger value that is still ≤ x can live.
Go left past a larger node
If the value is greater than x, move to the left child without saving anything. This node and its whole right subtree are too large.
Return the saved value
When the cursor becomes null, return ans. It is the largest value ≤ x on the search path, and every subtree the walk skipped held only values that were too large or too small to beat it.
Code in Python, C++ and Java
ans = -1, which is the answer if nothing qualifies.ans = -1, which is the answer if nothing qualifies.ans = -1, which is the answer if nothing qualifies.Common pitfalls
Returning the first value below x
if root.data < x:
return root.dataif root.data < x:
ans = root.data
root = root.rightThe first smaller value on the path is often not the largest one. In the tree [8,3,10,1,6,null,14,null,null,4,7,13] with x = 5, the walk meets 3 first, but 4 lies further down on its right and is the real floor.
Going left after saving a candidate
if root.data < x:
ans = root.data
root = root.leftif root.data < x:
ans = root.data
root = root.rightThis is the ceil walk's turn, copied by mistake. The left subtree holds only smaller values, so the walk moves away from x. In the same tree with x = 5 it saves 3, then 1, and returns 1 instead of 4.
Edge cases
x smaller than every valueEvery node is larger than x, so the walk only goes left, saves nothing and returns -1. In the tree above, x = 0 visits 8, 3 and 1.
x larger than every valueEvery node is smaller, so the walk saves each one and keeps going right. The answer is the largest value in the tree: the rightmost node.
Complexity
ans and the cursor are stored.Floor in BST FAQ
Can this be done with recursion?
If the node is null, return -1. If its value equals x, return it. If it is larger than x, return the answer from the left child. Otherwise ask the right child, and return the node's own value if the right side gave -1. The logic matches the loop, but the call stack adds O(h) memory.
Is the floor the same as the inorder predecessor?
Not quite. The predecessor of a node is the largest value strictly smaller than it, so equality does not count. The floor of x allows the value x itself, and x does not even have to be in the tree.