GeeksforGeeks Easy

Ceil in BST

Ceil in BST is a GFG problem (Easy). Given the root of a binary search tree and a number x, return the ceil of x: the smallest value in the tree that is ≥ x. If every value is smaller than x, return -1.

  • If x is in the tree, the ceil is x itself.
  • The expected solution runs in O(height) time with O(1) extra space, so you cannot list all the values first.
Constraints
  • The tree is a binary search tree with at least one node.
  • 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

The ceil lies on the path you would follow to search for x. At each node, one comparison tells you which side can still hold it.

  • Node smaller than x: it is too small, and so is its whole left subtree. Only the right side can help.
  • Node larger than x: it is a valid ceil, but a smaller valid one may sit in its left subtree. Remember it, then look left.

The last node remembered is the closest one above x, so you walk down once and never come back up.

How to spot this pattern

"Closest value at or above (or below) a target" in a BST is a search walk that remembers the best candidate seen. The same walk with the directions flipped gives Floor in BST, and it also answers inorder successor and closest value in a BST.

03

Approach

Try it first

Before reading on: in the tree [10, 5, 11, 4, 7, null, null, null, null, null, 8], with x = 6, which nodes do you visit, and which of them could be the answer? What should you return when x = 12?

1

Start with no answer

Set ceil = -1 and point a cursor at the root. If nothing in the tree turns out to be ≥ x, this -1 is already the right answer.

2

Compare and move one level down

While the cursor is not null, compare its value v with x:

  • v == x: return x. Nothing ≥ x can be smaller.
  • v > x: set ceil = v, then move left to look for a smaller value that is still ≥ x.
  • v < x: move right, towards larger values.
3

Return the saved value

When the cursor falls off the tree, ceil holds the smallest value ≥ x that the walk saw. Every node skipped was on a side that could not hold a closer one, so it is the answer.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def findCeil(self, root, x):
▶3 ceil = -1
▶4 while root:
▶5 if root.data == x:
▶6 return x
▶7 if root.data > x:
▶8 ceil = root.data
▶9 root = root.left
▶10 else:
▶11 root = root.right
▶12 return ceil
51723nodexnone3ceil-1ceil = -1, start at the root
x3
ceil-1nothing ≥ x yet
Start. No value ≥ 3 has been seen yet, so ceil = -1. If the walk never finds one, -1 is already the right answer.
51723nodex5>3ceil55 > 3 → ceil = 5, go left
x3
ceil5smallest ≥ x so far
5 is above 3, so it is a valid ceil: save it. Everything to its right is even larger and cannot beat it, so that side is ruled out. A closer value could only be in the left subtree, so move left to 1.
51723nodex1<3ceil51 < 3 → too small, go right
x3
ceil5smallest ≥ x so far
1 is below 3, so it cannot be the ceil, and neither can anything in its left subtree, which is smaller still. Only the right side can hold larger values, so move right to 2.
51723nodex2<3ceil52 < 3 → too small, go right
x3
ceil5smallest ≥ x so far
2 is below 3, so it cannot be the ceil, and neither can anything in its left subtree, which is smaller still. Only the right side can hold larger values, so move right to 3.
51723nodex3=3ceil53 = x → return 3
x3
ceil5smallest ≥ x so far
answer3
3 equals x. No value can be ≥ 3 and smaller than 3, so the ceil is 3 itself. Return it straight away; the saved 5 is no longer needed.
05

Common pitfalls

Returning the first value above x

✗ Wrong
if root.data > x:
    return root.data
✓ Right
if root.data > x:
    ceil = root.data
    root = root.left

The first node above x is a valid ceil but not always the smallest. With x = 6 in [10, 5, 11, 4, 7, null, null, null, null, null, 8], this returns 10, but 7 is closer.

Going right after saving a candidate

✗ Wrong
if root.data > x:
    ceil = root.data
    root = root.right
✓ Right
if root.data > x:
    ceil = root.data
    root = root.left

Everything to the right is larger than the value just saved, so it can never beat it. A closer ceil can only be in the left subtree.

06

Edge cases

x smaller than every value

Every node is ≥ x, so the walk saves each one and keeps going left. The answer is the smallest value in the tree.

07

Complexity

Time
O(h)
Space
O(1)
One step down per comparison, so at most h + 1 nodes: O(log n) on a balanced tree, O(n) on a tree shaped like a chain.
08

Ceil in BST FAQ

Can this be done with recursion?

Yes. If the node is null, return -1. If it equals x, return it. If it is smaller than x, return the result for the right child. Otherwise ask the left child; if that gives -1, this node is the ceil, else the left child's answer is. It takes the same O(h) time, but the call stack adds O(h) memory.

Why not collect the values with an inorder traversal?

Inorder gives the values in sorted order, and the first one ≥ x is the ceil. That works, but it visits every node and stores them: O(n) time and O(n) space, where the walk needs only O(h) time and no extra memory.

What changes for floor in a BST?

The floor is the largest value ≤ x. Flip both directions: when a node is smaller than x, save it and go right; when it is larger, go left. An exact match is both the floor and the ceil in a BST.