883. Floor Value in BST

EasyTreesBSTBinary Search

Given the root of a binary search tree and a target value, return the floor of the target — the largest key less 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 floor value, or -1.

Examples

Example 1
Input: [8,3,10,1,6,null,14] | 5
Output: 3
Explanation: Largest key <= 5.
Example 2
Input: [8,3,10,1,6,null,14] | 6
Output: 6
Explanation: Exact match is its own floor.
Example 3
Input: [8,3,10,1,6,null,14] | 0
Output: -1
Explanation: No key <= 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →