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

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

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

Introduction

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

This problem teaches:

  1. BST traversal
  2. Recursive searching
  3. Decision making using BST properties
  4. Tree optimization
  5. Interview-level recursion concepts

The main challenge is understanding how BST ordering helps us efficiently locate the smallest value greater than or equal to a target number.

Problem Link

🔗 GeeksforGeeks – Ceil in BST

Problem Statement

Given a Binary Search Tree and an integer:

x

find the:

Ceil(x)

The ceil of a number is:

The smallest value in the BST that is greater than or equal to x.

If no such value exists:

return -1

Example 1

Input

root = [5,1,7,N,2,N,N,N,3]
x = 3

Output

3

Explanation

Since:

3 exists in the BST

the ceil is:

3

Example 2

Input

root = [10,5,11,4,7,N,N,N,N,N,8]
x = 6

Output

7

Explanation

The smallest value greater than or equal to:

6

is:

7

Key Observation

Binary Search Trees follow:

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

This allows efficient searching.

Intuition

Suppose:

x = 6

and current node is:

10

Since:

10 > 6

this node can potentially be the answer.

But maybe a smaller valid ceil exists in the left subtree.

So:

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

Important BST Logic

If:

root.data == x

We found exact ceil.

Return immediately.

If:

root.data > x

Current node can be ceil.

Move left to find smaller possible ceil.

If:

root.data < x

Current node cannot be ceil.

Move right.

Brute Force Approach

Idea

Traverse the entire BST:

  1. Store all values greater than or equal to x
  2. Return minimum among them

Brute Force Complexity

Time Complexity

O(N)

Space Complexity

O(N)

If storing elements.

Optimized BST Recursive Approach

Using BST properties:

  1. Ignore unnecessary branches
  2. Search intelligently
  3. Reduce traversal work

Java Solution

class Solution {

int c = -1;

int solve(Node root, int x, int c) {

if(root == null) return c;

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

if(root.data > x) {

c = root.data;

return solve(root.left, x, c);

} else {

return solve(root.right, x, c);
}
}

int findCeil(Node root, int x) {

return solve(root, x, c);
}
}

How the Solution Works

The recursion maintains:

current best ceil candidate

Whenever:

root.data > x

update ceil candidate.

Then search left subtree for smaller valid answer.

Dry Run

Input

10
/ \
5 11
/ \
4 7
\
8

x = 6

Step 1

Current node:

10

Since:

10 > 6

Possible ceil:

10

Move left.

Step 2

Current node:

5

Since:

5 < 6

Move right.

Step 3

Current node:

7

Since:

7 > 6

Update ceil:

7

Move left.

Step 4

Left child is null.

Return:

7

Final Answer

7

Optimized Iterative Approach

You can also solve this iteratively.

Iterative Java Solution

class Solution {

int findCeil(Node root, int x) {

int ceil = -1;

while(root != null) {

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

if(root.data > x) {

ceil = root.data;
root = root.left;

} else {

root = root.right;
}
}

return ceil;
}
}

Why Iterative is Better

Iterative solution avoids recursion stack.

Better for:

  1. Large trees
  2. Memory optimization
  3. Interview follow-up questions

Time Complexity Analysis

Best Case

O(log N)

Balanced BST.

Worst Case

O(N)

Skewed BST.

Space Complexity

Recursive

O(H)

Recursion stack.

Iterative

O(1)

Extra space.

Interview Explanation

In interviews explain:

Since BST maintains sorted ordering, we can intelligently move left or right. Whenever a node is greater than x, it becomes a potential ceil candidate.

This demonstrates:

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

Common Mistakes

1. Traversing Entire Tree

Unnecessary because BST already provides ordering.

2. Not Updating Ceil Properly

Always update:

ceil = root.data

before moving left.

3. Returning First Greater Element

Need the:

smallest greater value

not just any greater value.

4. Ignoring Exact Match

If:

root.data == x

return immediately.

FAQs

Q1. What is ceil in BST?

Smallest value greater than or equal to x.

Q2. Why move left after finding larger value?

To search for a smaller valid ceil.

Q3. Can this be solved iteratively?

Yes.

Iterative solution is highly optimized.

Q4. What if ceil does not exist?

Return:

-1

Related BST Problems

Practice these next:

  1. Search in BST
  2. Insert into BST
  3. Kth Smallest in BST
  4. Lowest Common Ancestor in BST

Conclusion

Ceil in BST is an excellent problem for learning:

  1. BST traversal
  2. Recursive decision making
  3. Search optimization
  4. Interview tree logic

The key insight is:

Whenever a node is greater than x, it becomes a potential answer, but a smaller valid ceil may still exist in the left subtree.

Mastering this concept makes many BST interview problems significantly easier.

Ai Assistant Kas