GeeksforGeeks Medium

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 x itself is in the tree, the floor is x.
  • 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.

Constraints
  • 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.
bstbinary-search
Open on GeeksforGeeks ↗
02

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 to x. 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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

5

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def floor(self, root, x):
▶3 ans = -1
▶4 while root:
▶5 if root.data == x:
▶6 return x
▶7 if root.data < x:
▶8 ans = root.data
▶9 root = root.right
▶10 else:
▶11 root = root.left
▶12 return ans
134678101314134678101314x = 5no floor yet, start at the root 8
x5
ans-1no floor found yet
The floor of 5 is the largest value ≤ 5. Below the tree, the values sit on a number line and the shaded band is every value the walk can still reach: all of them, for now. Start with ans = -1, which is the answer if nothing qualifies.
134678101314134678101314x = 58 > 5 → too big, go left
node8above x
ans-1still none
8 is larger than 5, so it cannot be the floor, and its right subtree is larger still. The band now ends below 8. Only the left subtree can help, so go to 3.
134678101314134678101314x = 53 < 5 → save 3, go right
node3below x
ans3best so far
3 is ≤ 5, so it could be the floor: save it. Its left subtree holds only smaller values, which can never beat 3, so the band now starts above 3. Something between 3 and 5 can only be in the right subtree, so go to 6.
134678101314134678101314x = 56 > 5 → too big, go left
node6above x
ans3unchanged
6 is larger than 5, so it cannot be the floor, and its right subtree is larger still. The band now ends below 6. Only the left subtree can help, so go to 4.
134678101314134678101314x = 54 < 5 → save 4, go right
node4below x
ans4best so far
4 is ≤ 5, so it could be the floor: save it. Its left subtree holds only smaller values, which can never beat 4, so the band now starts above 4. There is no right child, so no closer value exists.
134678101314134678101314x = 5fell off the tree → return 4
answer4
The cursor is null. Every value outside the band was ruled out by one comparison, and no value lies inside the band beyond the saved one, so 4 is the floor of 5.
05

Common pitfalls

Returning the first value below x

✗ Wrong
if root.data < x:
    return root.data
✓ Right
if root.data < x:
    ans = root.data
    root = root.right

The 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

✗ Wrong
if root.data < x:
    ans = root.data
    root = root.left
✓ Right
if root.data < x:
    ans = root.data
    root = root.right

This 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.

06

Edge cases

x smaller than every value

Every 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 value

Every 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.

07

Complexity

Time
O(h)
Space
O(1)
h is the height of the tree. The cursor moves down one level per loop, so it visits at most h + 1 nodes. That is O(log n) for a balanced tree and O(n) for a tree shaped like a chain. Only ans and the cursor are stored.
08

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.