Given the root of a binary search tree and an integer k, return true if there exist two distinct nodes whose values add up to k. Pattern focus: BST Search. The BST property helps you search the value space efficiently while avoiding duplicate node usage.
root = binary tree root, k = target sum
true if two distinct nodes sum to k
Example 1:
Input:
root = [5,3,6,2,4,null,7] k = 9
Output:
true
Explanation:
2 + 7 = 9, so a valid pair exists.
Example 2:
Input:
root = [5,3,6,2,4,null,7] k = 28
Output:
false
Explanation:
No two distinct nodes add up to 28.