Trees · Coding problem
Ceil in a BST
A free coding problem from the BST Floor and Ceil skill. Read it here, then solve it in the app in Java or Python.
- Easy
- Coding problem
- Free
- Java and Python
The problem
Given the root of a binary search tree and a key, find the ceil of the key: the smallest node value that is greater than or equal to the key. If no such value exists, return -1.
Input format
<tree in heap-array format>
key
Example
Input:
7
8 5 10 3 6 null 12
7
Output: 8
Constraints
1 <= number of nodes <= 50000 <= node value, key <= 10^9
How it's graded
- Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
More problems in BST Floor and Ceil
Solve it Ceil in a BST free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime