Ceil in BST

Easy
1896.

Ceil in BST

Ceil in BST is an easy coding problem on binary search and trees, asked in Amazon and Microsoft interviews.

You are given the root of a binary search tree with distinct values and an integer x.

Return the ceil of x in the tree: the smallest node value that is greater than or equal to x. If every value is smaller than x, return -1.

Example 1

Input
root = [8,4,12,2,6,10,14], x = 5
Output
6
Explanation
The values not below 5 are 6, 8, 10, 12 and 14; the smallest is 6.

Example 2

Input
root = [8,4,12,2,6,10,14], x = 14
Output
14
Explanation
14 is in the tree, so it is its own ceil.

Example 3

Input
root = [8,4,12,2,6,10,14], x = 15
Output
-1
Explanation
Every value is below 15.

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^5
  • all node values are distinct
  • 1 <= x <= 10^5
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * Definition for a binary tree node (provided by the judge — do not redefine it):
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} x
 * @return {number}
 */
var findCeil = function(root, x) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Ceil in BST — Solution in Python, Java & C++

Ceil in BST solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on binary search and trees.

Ceil in BST is an easy coding problem on binary search and trees, asked in Amazon and Microsoft interviews.

  • Difficulty: Easy
  • Topics: Binary Search, Trees
  • Asked at: Amazon, Microsoft
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given the root of a binary search tree with distinct values and an integer x.

Return the ceil of x in the tree: the smallest node value that is greater than or equal to x. If every value is smaller than x, return -1.

Example 1

Input: root = [8,4,12,2,6,10,14], x = 5
Output: 6
Explanation: The values not below 5 are 6, 8, 10, 12 and 14; the smallest is 6.

Example 2

Input: root = [8,4,12,2,6,10,14], x = 14
Output: 14
Explanation: 14 is in the tree, so it is its own ceil.

Example 3

Input: root = [8,4,12,2,6,10,14], x = 15
Output: -1
Explanation: Every value is below 15.

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^5
  • all node values are distinct
  • 1 <= x <= 10^5

Hints for Ceil in BST

  1. Hint 1

    Walk down from the root as in a search; the ordering tells you which side can hold better candidates.

  2. Hint 2

    If a node's value is at least x, it is a candidate — and anything smaller that still qualifies is in its left subtree.

  3. Hint 3

    If a node's value is below x, neither it nor its left subtree qualifies: go right. Keep the last candidate seen.

How to solve Ceil in BST

Search for x as usual. Every node where the walk turns left has a value ≥ x and is a candidate; each later candidate is smaller than the previous one, so the last one recorded is the ceil.

Approach

  1. Set ceil = -1 and start at the root.
  2. If the node's value equals x, return it.
  3. If it is greater than x, record it as ceil and move left; otherwise move right.
  4. When the walk falls off the tree, return ceil.

Why it works

When a node's value v is above x, its right subtree holds only values above v — never a better ceil — so the search narrows to the left. When v is below x, the node and its whole left subtree are below x and are excluded. The candidates recorded shrink each time, ending at the smallest value ≥ x.

Complexity

  • Time — O(h)
  • Space — O(1)

Pitfalls

  • Return -1, not the largest value, when x exceeds every value in the tree.
  • An exact match is its own ceil — return it at once.

Reference solution

Python

from typing import Optional

def findCeil(root: Optional[TreeNode], x: int) -> int:
    ceil = -1
    node = root
    while node is not None:
        if node.val == x:
            return x
        if node.val > x:
            ceil = node.val
            node = node.left
        else:
            node = node.right
    return ceil

Java

public static int findCeil(TreeNode root, int x) {
    int ceil = -1;
    TreeNode node = root;
    while (node != null) {
        if (node.val == x) return x;
        if (node.val > x) { ceil = node.val; node = node.left; }
        else node = node.right;
    }
    return ceil;
}

C++

int findCeil(TreeNode* root, int x) {
    int ceil = -1;
    TreeNode* node = root;
    while (node) {
        if (node->val == x) return x;
        if (node->val > x) { ceil = node->val; node = node->left; }
        else node = node->right;
    }
    return ceil;
}

JavaScript

var findCeil = function(root, x) {
    var ceil = -1, node = root;
    while (node !== null) {
        if (node.val === x) return x;
        if (node.val > x) { ceil = node.val; node = node.left; }
        else node = node.right;
    }
    return ceil;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 189 binary search problems · the whole catalogue

Learn the technique: Binary Search · Binary Trees and Tree Traversals