881. Inorder Successor in BST

EasyTreesBSTBinary Search

Given the root of a binary search tree and a target value, return the in-order successor of the target — the smallest key strictly greater than the target. Return -1 if no successor exists. The target may or may not be present. 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 successor value, or -1.

Examples

Example 1
Input: [8,3,10,1,6,null,14] | 6
Output: 8
Explanation: Smallest key greater than 6.
Example 2
Input: [8,3,10,1,6,null,14] | 14
Output: -1
Explanation: No successor for the maximum.
Example 3
Input: [5] | 1
Output: 5
Explanation: 5 is the next greater key.

Constraints

Asked by

MicrosoftMetaGoogleAmazon
Solve this problem in the editor →