Two Sum in a BST
You are given the root of a binary tree. Return True if there are two different nodes in the BST whose values add up to k, otherwise False. A node cannot be used twice. In a binary search tree every value in a left subtree is smaller than its node and every value in a right subtree is larger.
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
root [5,3,6,2,4,null,7]
k 9
returns true
2 + 7 = 9.
Example 2
root [5,3,6,2,4,null,7]
k 28
returns false
No pair adds to 28.
Constraints
0 <= number of nodes <= 100-1000 <= node.val <= 1000- The tree is a valid binary search tree.
-2000 <= k <= 2000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.