884. Ceil Value in BST

EasyTreesBSTBinary Search

Given the root of a binary search tree and a target value, return the ceil of the target — the smallest key greater than or equal to the target. Return -1 if no such key exists. The input gives a level-order BST array and the target separated by ' | '.

Input: A level-order BST array and the target, separated by ' | '.

Output: Integer — the ceil value, or -1.

Examples

Example 1
Input: [8,3,10,1,6,null,14] | 5
Output: 6
Explanation: Smallest key >= 5.
Example 2
Input: [8,3,10,1,6,null,14] | 6
Output: 6
Explanation: Exact match is its own ceil.
Example 3
Input: [8,3,10,1,6,null,14] | 15
Output: -1
Explanation: No key >= 15.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →