Ceil in BST

Easy Trees & BST Miscellaneous BST Questions Original on GeeksforGeeks

Ceil in BST is a easy trees & bst problem solved with the miscellaneous bst questions pattern. The best approach, optimal (walk down the bst), runs in O(h) time and O(1) space. Below are 2 approaches in Java, from traverse everything up.

Problem

Given a BST and a value x, return the ceil of x in the tree: the smallest value that is greater than or equal to x, or -1 if there is none.

Examples

Example 1

Input
root = [8, 5, 9, 1, 7, null, 10, null, 2, 6], x = 3
Output
5

Example 2

Input
same tree, x = 11
Output
-1

Constraints

  • The tree has 1 to 10^5 nodes.
  • Return -1 if no value is >= x.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Traverse everythingO(n)O(h)
Optimal (walk down the BST)O(h)O(1)

1Traverse everything

TimeO(n)
SpaceO(h)

Visit every node and keep the smallest value that is still >= x.

  1. DFS; track the minimum value >= x.
Java
class Solution {
    int findCeil(TreeNode root, int x) {
        int best = Integer.MAX_VALUE;
        Deque<TreeNode> st = new ArrayDeque<>();
        if (root != null) st.push(root);
        while (!st.isEmpty()) {
            TreeNode n = st.pop();
            if (n.val >= x) best = Math.min(best, n.val);
            if (n.left != null) st.push(n.left);
            if (n.right != null) st.push(n.right);
        }
        return best == Integer.MAX_VALUE ? -1 : best;
    }
}

2Optimal (walk down the BST)

TimeO(h)
SpaceO(1)

Equal returns immediately. If the node is smaller than x, the ceil is to the right. Otherwise record it and look left for a smaller candidate.

  1. ans = -1.
  2. val == x → x. val < x → right. Else ans = val, left.
Java
class Solution {
    int findCeil(TreeNode root, int x) {
        int ans = -1;
        while (root != null) {
            if (root.val == x) return x;
            if (root.val < x) root = root.right;
            else { ans = root.val; root = root.left; }
        }
        return ans;
    }
}

Edge cases to test

  • x equals a node value
  • x above the maximum

Hints

Hint 1

A node >= x is a candidate; a smaller candidate may exist to its left.

FAQ

What is the best time complexity for Ceil in BST?

Optimal (walk down the BST) runs in O(h) time and O(1) extra space.

Which pattern does Ceil in BST use?

It is a trees & bst problem that uses the miscellaneous bst questions pattern. Other problems with the same pattern: Inorder Successor in BST, Inorder predecessor, Floor in BST.

Is there a brute force solution for Ceil in BST?

Yes. Traverse everything takes O(n) time and O(h) space. Visit every node and keep the smallest value that is still = x.

Which edge cases should I test for Ceil in BST?

x equals a node value; x above the maximum.