Floor in BST – Java Recursive Binary Search Tree Solution with Dry Run

Learn how to solve the Floor in BST problem using an optimized recursive BST approach in Java. Includes intuition, brute force solution, optimized recursion, dry run, time complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
1 views
LinkedInGithubX
0
0
Floor in BST – Java Recursive Binary Search Tree Solution with Dry Run
Listen to articleAudio version
Ad
Advertisement

Introduction

The Floor in BST problem is one of the most important Binary Search Tree interview questions for beginners.

This problem helps you understand:

  1. BST traversal
  2. Recursive searching
  3. Tree optimization
  4. Decision making using BST properties
  5. Efficient searching techniques

The main idea is to efficiently find the:

Greatest value smaller than or equal to k.

Problem Link

🔗 GeeksforGeeks – Floor in BST

Problem Statement

Given the root of a Binary Search Tree and an integer:

k

find the:

Floor(k)

The floor of a number is:

The greatest value in the BST that is less than or equal to k.

If no such value exists:

return -1

Example 1

Input

root = [10,7,15,2,8,11,16]
k = 14

Output

11

Explanation

The largest value smaller than or equal to:

14

is:

11

Example 2

Input

root = [5,2,12,1,3,9,21,null,null,null,null,null,null,19,25]
k = 24

Output

21

Key Observation

Binary Search Tree follows:

Left subtree -> smaller values
Right subtree -> greater values

This ordering allows optimized searching.

Intuition

Suppose:

k = 14

and current node is:

10

Since:

10 < 14

this node can potentially be the floor.

But maybe there exists a larger valid floor in the right subtree.

So:

  1. Store current node as possible answer
  2. Move right

BST Logic

Case 1

If:

root.data == k

then exact floor exists.

Return immediately.

Case 2

If:

root.data > k

current node cannot be floor.

Move left.

Case 3

If:

root.data < k

current node becomes possible floor.

Move right to search for larger valid answer.

Brute Force Approach

Idea

Traverse the entire BST:

  1. Store all values smaller than or equal to k
  2. Return maximum among them

Brute Force Complexity

Time Complexity

O(N)

Space Complexity

O(N)

If storing nodes.

Optimized Recursive BST Approach

Using BST properties:

  1. Ignore unnecessary branches
  2. Search efficiently
  3. Reduce traversal operations

Java Solution

class Solution {

int f = -1;

public int findMaxFork(Node root, int k) {

if(root == null)
return f;

if(root.data == k)
return root.data;

if(root.data > k) {

return findMaxFork(root.left, k);

} else {

f = root.data;

return findMaxFork(root.right, k);
}
}
}

How the Solution Works

Whenever:

root.data < k

the node becomes a possible floor candidate.

But there may exist a larger valid floor in the right subtree.

So:

  1. Save current node
  2. Move right

Dry Run

Input

10
/ \
7 15
/ \ / \
2 8 11 16

k = 14

Step 1

Current node:

10

Since:

10 < 14

Possible floor:

10

Move right.

Step 2

Current node:

15

Since:

15 > 14

Move left.

Step 3

Current node:

11

Since:

11 < 14

Update floor:

11

Move right.

Step 4

Right child is null.

Return:

11

Final Answer

11

Optimized Iterative Approach

The same problem can also be solved iteratively.

Iterative Java Solution

class Solution {

int findFloor(Node root, int k) {

int floor = -1;

while(root != null) {

if(root.data == k) {
return root.data;
}

if(root.data > k) {

root = root.left;

} else {

floor = root.data;
root = root.right;
}
}

return floor;
}
}

Why Iterative Approach is Better

Advantages:

  1. Avoids recursion stack
  2. More memory efficient
  3. Better for skewed trees
  4. Preferred in some interviews

Time Complexity Analysis

Best Case

O(log N)

Balanced BST.

Worst Case

O(N)

Skewed BST.

Space Complexity

Recursive

O(H)

where H is tree height.

Iterative

O(1)

extra space.

Interview Explanation

In interviews explain:

Whenever we encounter a node smaller than k, it becomes a possible floor candidate. Then we move right to search for a larger valid floor.

This demonstrates:

  1. BST property understanding
  2. Search optimization
  3. Recursive reasoning
  4. Efficient traversal

Common Mistakes

1. Traversing Entire Tree

BST ordering already helps reduce search space.

2. Forgetting to Update Floor

Always update:

floor = root.data

before moving right.

3. Returning First Smaller Value

Need the:

largest smaller value

not any smaller value.

4. Ignoring Exact Match

If:

root.data == k

return immediately.

FAQs

Q1. What is floor in BST?

Largest value smaller than or equal to k.

Q2. Why move right after finding smaller value?

To search for a larger valid floor.

Q3. Can this be solved iteratively?

Yes.

Iterative solution is highly optimized.

Q4. What if floor does not exist?

Return:

-1

Related BST Problems

Practice these next:

  1. Ceil in BST
  2. Search in BST
  3. Insert into BST
  4. Validate BST
  5. Lowest Common Ancestor in BST

Conclusion

Floor in BST is an excellent problem for understanding:

  1. BST traversal
  2. Recursive searching
  3. Search space optimization
  4. Interview tree concepts

The key insight is:

Whenever a node is smaller than k, it becomes a possible floor candidate, but a larger valid floor may still exist in the right subtree.

Mastering this logic makes many BST interview problems much easier.

Ai Assistant Kas