Two Sum IV - Input in a BST
Tree
easy
Score: 20
Given the root
of a Binary Search Tree and a target number k
return 1
if there exist two elements in the BST such that their sum is equal to the given target else return 0
.
Class TreeNode:
val (int)
left (TreeNode)
right (TreeNode)
Input Format:
First Parameter - TreeNode root
Second Parameter - number k
Output Format:
Return the number.
Example 1:
Input:
root = [11 7 14 3 9]
k = 17
Output:
1
Example 2:
Input:
root = [11 7 14 3 9]
k = 200
Output:
0
Constraints:
- The number of nodes is in the range [1,104]
- -104 <=
node.val
<= 104. root
is guaranteed to be a valid binary search tree.- -105 <=
k
<= 105 - Expected Time Complexity: O(N).
- Expected Space Complexity: O(logN).