Floor in Binary Search Tree (BST)
Last Updated :
22 Oct, 2025
Given a Binary Search Tree and a number x, we have to find the floor of x in the given BST, where floor means the greatest value node of the BST which is smaller than or equal to x. if x is smaller than the smallest node of BST then return -1.
Examples:
Input:
Output: 55
Explanantion:
[Expected Approach - 1] Using Recursion - O(h) Time and O(h) Space
The idea is to find the floor in a BST by traversing recursively: if the node’s value is greater than the key, move left; if it is less than or equal to the key, record it and move right.
Follow the given steps to solve the problem:
- Start at the root node.
- If root->data == x, the floor of x is equal to the root.
- Else if root->data > x, the floor must lie in the left subtree.
- Otherwise, search in the right subtree for a value less than or equal to x, but if not found, the root itself is the floor.
C++
// C++ implementation to find floor of given
// number x in BST
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *left;
Node *right;
Node(int x) {
data = x;
left = nullptr;
right = nullptr;
}
};
// This function is used to find floor
// of given number x
int floor(Node* root, int x) {
// Base case: return -1 if no floor found
if (root == nullptr) {
return -1;
}
// If the root's data is equal to x,
// we've found the floor
if (root->data == x) {
return root->data;
}
// If root's data is greater than x,
// search in the left subtree
if (root->data > x) {
return floor(root->left, x);
}
// Else, search in the right subtree
// and compare with current root
int floorValue = floor(root->right, x);
// If the right subtree returns
//a valid floor, return that
// Otherwise, return the current root's data
return (floorValue <= x && floorValue != -1)
? floorValue : root->data;
}
int main() {
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
Node* root = new Node(10);
root->left = new Node(5);
root->right = new Node(15);
root->right->left = new Node(12);
root->right->right = new Node(30);
int x = 14;
cout << floor(root, x) << endl;
return 0;
}
C
// C implementation to find floor of given
// number x in BST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// This function is used to find floor
// of given number x
int floor(struct Node* root, int x) {
// Base case: return -1 if no floor found
if (root == NULL) {
return -1;
}
// If the root's data is equal to x,
// we've found the floor
if (root->data == x) {
return root->data;
}
// If root's data is greater than x,
// search in the left subtree
if (root->data > x) {
return floor(root->left, x);
}
// Else, search in the right subtree
// and compare with current root
int floorValue = floor(root->right, x);
// If the right subtree returns
// a valid floor, return that
// Otherwise, return the current root's data
return (floorValue <= x && floorValue != -1)
? floorValue : root->data;
}
struct Node* createNode(int x) {
struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int main() {
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
struct Node* root = createNode(10);
root->left = createNode(5);
root->right = createNode(15);
root->right->left = createNode(12);
root->right->right = createNode(30);
int x = 14;
printf("%d\n", floor(root, x));
return 0;
}
Java
// Java implementation to find floor of given
// number x in BST
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = null;
right = null;
}
}
// This function is used to find floor
// of given number x
class GfG {
static int floor(Node root, int x) {
// Base case: return -1 if no floor found
if (root == null) {
return -1;
}
// If the root's data is equal to x,
// we've found the floor
if (root.data == x) {
return root.data;
}
// If root's data is greater than x,
// search in the left subtree
if (root.data > x) {
return floor(root.left, x);
}
// Else, search in the right subtree
// and compare with current root
int floorValue = floor(root.right, x);
// If the right subtree returns a valid floor,
// return that, otherwise return the current root's data
return (floorValue <= x && floorValue != -1)
? floorValue : root.data;
}
public static void main(String[] args) {
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.right.left = new Node(12);
root.right.right = new Node(30);
int x = 14;
System.out.println(floor(root, x));
}
}
Python
# Python implementation to find floor of given
# number x in BST
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# This function is used to find floor
# of given number x
def find_floor(root, x):
# Base case: return -1 if no floor found
if root is None:
return -1
# If the root's data is equal to x,
# we've found the floor
if root.data == x:
return root.data
# If root's data is greater than x,
# search in the left subtree
if root.data > x:
return find_floor(root.left, x)
# Else, search in the right subtree
# and compare with current root
floor_value = find_floor(root.right, x)
# If the right subtree returns a valid floor,
# return that, otherwise return the current root's data
return floor_value if floor_value <= x \
and floor_value != -1 else root.data
if __name__ == "__main__":
# Representation of the given tree
# 10
# / \
# 5 15
# / \
# 12 30
root = Node(10)
root.left = Node(5)
root.right = Node(15)
root.right.left = Node(12)
root.right.right = Node(30)
x = 14
print(find_floor(root, x))
C#
// C# implementation to find floor of given
// number x in BST
using System;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
// This function is used to find floor
// of given number x
class GfG {
static int FindFloor(Node root, int x) {
// Base case: return -1 if no floor found
if (root == null) {
return -1;
}
// If the root's data is equal to x,
// we've found the floor
if (root.data == x) {
return root.data;
}
// If root's data is greater than x,
// search in the left subtree
if (root.data > x) {
return FindFloor(root.left, x);
}
// Else, search in the right subtree
// and compare with current root
int floorValue = FindFloor(root.right, x);
// If the right subtree returns a valid floor,
// return that, otherwise return the current root's data
return (floorValue <= x && floorValue != -1)
? floorValue : root.data;
}
static void Main(string[] args) {
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.right.left = new Node(12);
root.right.right = new Node(30);
int x = 14;
Console.WriteLine(FindFloor(root, x));
}
}
JavaScript
// JavaScript implementation to find floor of given
// number x in BST
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// This function is used to find floor
// of given number x
function findFloor(root, x) {
// Base case: return -1 if no floor found
if (root === null) {
return -1;
}
// If the root's data is equal to x,
// we've found the floor
if (root.data === x) {
return root.data;
}
// If root's data is greater than x,
// search in the left subtree
if (root.data > x) {
return findFloor(root.left, x);
}
// Else, search in the right subtree
// and compare with current root
let floorValue = findFloor(root.right, x);
// If the right subtree returns a valid floor,
// return that, otherwise return the current root's data
return (floorValue <= x && floorValue !== -1)
? floorValue : root.data;
}
// Driver Code
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
let root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.right.left = new Node(12);
root.right.right = new Node(30);
let x = 14;
console.log(findFloor(root, x));
[Expected Approach - 2] Iterative Solution- O(h) Time and O(1) Space
The idea is to iteratively traverse the BST to find the greatest value smaller than or equal to x. If the current node's data is greater than x, move left. If it's smaller, update the floor value and move right. This process continues until the closest floor value is found.
Below is the implementation of the above approach:
C++
// C++ implementation to find floor of given
// number x in BST
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *left;
Node *right;
Node(int x) {
data = x;
left = nullptr;
right = nullptr;
}
};
// This function is used to find floor
// of given number x
int floor(Node* root, int x) {
// Initialize variable to store the floor value
int floorValue = -1;
while (root != nullptr) {
// If the root's data is equal to x,
// we've found the floor
if (root->data == x) {
return root->data;
}
// If root's data is greater than x,
// search in the left subtree
if (root->data > x) {
root = root->left;
}
else {
// Update floorValue and move to the
// right subtree
floorValue = root->data;
root = root->right;
}
}
return floorValue;
}
int main() {
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
Node* root = new Node(10);
root->left = new Node(5);
root->right = new Node(15);
root->right->left = new Node(12);
root->right->right = new Node(30);
int x = 14;
cout << floor(root, x) << endl;
return 0;
}
C
// C implementation to find floor of given
// number x in BST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
// This function is used to find floor
// of given number x
int floor(struct Node* root, int x) {
// Initialize variable to store the floor value
int floorValue = -1;
while (root != NULL) {
// If the root's data is equal to x,
// we've found the floor
if (root->data == x) {
return root->data;
}
// If root's data is greater than x,
// search in the left subtree
if (root->data > x) {
root = root->left;
}
else {
// Update floorValue and move to the
// right subtree
floorValue = root->data;
root = root->right;
}
}
// Return the floor value found
return floorValue;
}
struct Node* createNode(int x) {
struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int main() {
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
struct Node* root = createNode(10);
root->left = createNode(5);
root->right = createNode(15);
root->right->left = createNode(12);
root->right->right = createNode(30);
int x = 14;
printf("%d\n", floor(root, x));
return 0;
}
Java
// Java implementation to find floor of given
// number x in BST
class Node {
int data;
Node left;
Node right;
Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// This function is used to find floor
// of given number x
static int floor(Node root, int x) {
// Initialize variable to store the floor value
int floorValue = -1;
while (root != null) {
// If the root's data is equal to x,
// we've found the floor
if (root.data == x) {
return root.data;
}
// If root's data is greater than x,
// search in the left subtree
if (root.data > x) {
root = root.left;
}
else {
// Update floorValue and move to the
// right subtree
floorValue = root.data;
root = root.right;
}
}
// Return the floor value found
return floorValue;
}
public static void main(String[] args) {
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.right.left = new Node(12);
root.right.right = new Node(30);
int x = 14;
System.out.println(floor(root, x));
}
}
Python
# Python implementation to find floor of given
# number x in BST
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# This function is used to find floor
# of given number x
def floor(root, x):
# Initialize variable to store the floor value
floor_value = -1
while root is not None:
# If the root's data is equal to x,
# we've found the floor
if root.data == x:
return root.data
# If root's data is greater than x,
# search in the left subtree
if root.data > x:
root = root.left
else:
# Update floor_value and move to the
# right subtree
floor_value = root.data
root = root.right
return floor_value
if __name__ == "__main__":
# Representation of the given tree
# 10
# / \
# 5 15
# / \
# 12 30
root = Node(10)
root.left = Node(5)
root.right = Node(15)
root.right.left = Node(12)
root.right.right = Node(30)
x = 14
print(floor(root, x))
C#
// C# implementation to find floor of given
// number x in BST
using System;
class Node {
public int data;
public Node left;
public Node right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
// This function is used to find floor
// of given number x
class GfG {
static int Floor(Node root, int x) {
// Initialize variable to store the floor value
int floorValue = -1;
while (root != null) {
// If the root's data is equal to x,
// we've found the floor
if (root.data == x) {
return root.data;
}
// If root's data is greater than x,
// search in the left subtree
if (root.data > x) {
root = root.left;
}
else {
// Update floorValue and move to the
// right subtree
floorValue = root.data;
root = root.right;
}
}
// Return the floor value found
return floorValue;
}
static void Main() {
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.right.left = new Node(12);
root.right.right = new Node(30);
int x = 14;
Console.WriteLine(Floor(root, x));
}
}
JavaScript
// JavaScript implementation to find floor of given
// number x in BST
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// This function is used to find floor
// of given number x
function floor(root, x) {
// Initialize variable to store the floor value
let floorValue = -1;
while (root !== null) {
// If the root's data is equal to x,
// we've found the floor
if (root.data === x) {
return root.data;
}
// If root's data is greater than x,
// search in the left subtree
if (root.data > x) {
root = root.left;
}
else {
// Update floorValue and move to the
// right subtree
floorValue = root.data;
root = root.right;
}
}
return floorValue;
}
// Driver Code
// Representation of the given tree
// 10
// / \
// 5 15
// / \
// 12 30
const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.right.left = new Node(12);
root.right.right = new Node(30);
const x = 14;
console.log(floor(root, x));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem