DSA Tracker

Medium

Floor in BST

A medium BST problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
BST
Sheets
2
Core for
2 roles
Platform
GeeksforGeeks

The problem

Given the root of a binary search tree and an integer x, find the floor of x. The floor is the largest value in the BST that is less than or equal to x. If no such value exists, return -1.

Example 1

Input
root=[6,2,8,0,4,7,9,null,null,3,5], x=5
Output
5
Why
Value 5 exists in the tree, so the floor of 5 is 5.

Example 2

Input
root=[6,2,8,0,4,7,9,null,null,3,5], x=10
Output
9
Why
The largest value in the tree that is <= 10 is 9.

Example 3

Input
root=[1,0,2], x=1
Output
1
Why
Value 1 exists in the tree.

Constraints

  • The number of nodes is in the range [1, 104].
  • -109 <= Node.val <= 109
  • -109 <= x <= 109

How to think about it

Updated 2026-09-09

Every comparison with the current node gives a definitive choice: if the node matches x, it is its own floor; if it exceeds x, no candidate can exist in the right subtree so search must go left; if it is less than x, the current node is a viable candidate, and any closer match must lie strictly to the right.

Approaches, worst first

  1. Full inorder search

    time O(n) · space O(h)

    Traverse the entire tree inorder, recording the latest value that is <= x. While correct, it visits irrelevant subtrees and runs in linear time instead of taking advantage of branch pruning.

  2. Iterative candidate trackingWrite this one

    time O(h) · space O(1)

    Start with `floor = -1`. At each node, if `node.val == x`, return x immediately. If `node.val < x`, record `floor = node.val` and step right seeking a tighter lower bound. If `node.val > x`, step left without updating the candidate.

Where people lose marks · 2
  • Failing to record candidates on right branches. When `node.val < x`, moving to `root.right` without stashing `node.val` loses the closest floor if the right subtree contains only values greater than x.
  • Overwriting candidate when moving left. When `node.val > x`, updating floor clobbers a valid smaller floor with a value that exceeds x.

The theory behind it

BST — the ground this problem stands on. All BST problems

What BST is

A binary search tree is a binary tree that enforces a strict ordering rule at every node. Every value stored in a node's left branch must be smaller than the node itself, and every value in its right branch must be larger. Because this rule holds true everywhere down the tree, searching for a value does not require checking every branch. At each step, a single comparison lets you discard an entire half of the remaining nodes.

When to reach for it

Reach for a binary search tree when problems mention sorted tree structures, finding the kth smallest element, checking tree validity, or searching within a range of values. Signals include queries for the next greater element in a dynamic set, finding the lowest common ancestor in a sorted tree, or converting sorted arrays into height-balanced trees. When data must remain sorted while supporting continuous insertions and lookups, BST properties are the target.

How the pattern works

Use the ordering property to prune entire subtrees. When looking for a key, move left if the target is smaller than the current node, or move right if it is larger, stopping when values match or when reaching a null link. For validating a tree, do not merely compare a node to its immediate children; pass valid lower and upper value bounds down through recursive calls. Walking a valid binary search tree in-order visits all values in strictly increasing numerical order.

What each operation costs

OperationTime
search, insert, or delete on balanced treeO(log n)
search, insert, or delete on degenerate line treeO(n)
in-order traversal visiting all nodes in sorted orderO(n)
What usually goes wrong with BST
  • Validating a tree by checking only immediate children against the parent, missing violations where a node deep in the left subtree is larger than an ancestor higher up.
  • Allowing duplicate values on the wrong side when the problem specification requires strictly smaller values on the left and strictly greater on the right.
  • Deleting a node with two children by removing it directly instead of replacing its value with its in-order predecessor or successor and deleting that leaf instead.

Which roles need this problem

BST is a core topic for these 2 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 6 more roles, including Full-Stack Developer, Android Developer, iOS Developer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More BST problems

Problem set and role mapping as of .