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
xis in the tree, the ceil isxitself. - The expected solution runs in O(height) time with O(1) extra space, so you cannot list all the values first.
- 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.
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.
"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.
Approach
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?
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.
Compare and move one level down
While the cursor is not null, compare its value v with x:
v == x: returnx. Nothing ≥xcan be smaller.v > x: setceil = v, then move left to look for a smaller value that is still ≥x.v < x: move right, towards larger values.
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.
Code in Python, C++ and Java
ceil = -1. If the walk never finds one, -1 is already the right answer.ceil = -1. If the walk never finds one, -1 is already the right answer.ceil = -1. If the walk never finds one, -1 is already the right answer.Common pitfalls
Returning the first value above x
if root.data > x:
return root.dataif root.data > x:
ceil = root.data
root = root.leftThe 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
if root.data > x:
ceil = root.data
root = root.rightif root.data > x:
ceil = root.data
root = root.leftEverything 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.
Edge cases
x smaller than every valueEvery node is ≥ x, so the walk saves each one and keeps going left. The answer is the smallest value in the tree.
Complexity
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.