891. Two Sum in BST

EasyTreesBSTTwo PointersInorder

Given the root of a binary search tree and a target value, determine whether there exist two distinct nodes whose values sum to the target. Return true or false. 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: Boolean — true or false.

Examples

Example 1
Input: [5,3,6,2,4,null,7] | 9
Output: true
Explanation: 2 + 7 = 9.
Example 2
Input: [5,3,6,2,4,null,7] | 28
Output: false
Explanation: No pair sums to 28.
Example 3
Input: [2,1,3] | 4
Output: true
Explanation: 1 + 3 = 4.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →