13. Pair Sum in BST
The problem can be found at the following link: Question Link
Problem Description
Given a Binary Search Tree (BST) and a target value, check whether there exists a pair of nodes in the BST whose sum equals the given target.
Examples
Example 1:
Input:
7
/ \
3 8
/ \ \
2 4 9
Target = 12
Output:
True
Explanation:
In the given BST, there exist two nodes (8 and 4) that sum up to 12.
Example 2:
Input:
9
/ \
5 10
/ \ \
2 6 12
Target = 23
Output:
False
Explanation:
No pair of nodes in the BST sum up to 23.
Constraints:
$(1 \leq \text{Number of Nodes} \leq 10^5)$
$(1 \leq \text{target} \leq 10^6)$
My Approach
Optimized Two-Pointer on Inorder Traversal (O(N)
Time, O(N)
Space)
O(N)
Time, O(N)
Space)Convert BST to sorted array using inorder traversal.
Use two-pointer approach (left and right) to find the target sum efficiently.
Algorithm Steps:
Perform an inorder traversal and store the BST elements in a sorted list.
Use two pointers (
left
at the start,right
at the end) and check the sum:If
arr[left] + arr[right] == target
, returntrue
.If
arr[left] + arr[right] < target
, moveleft++
.If
arr[left] + arr[right] > target
, moveright--
.
If no such pair exists, return
false
.
Time and Auxiliary Space Complexity
Expected Time Complexity:
O(N)
, since we traverse each node once.Expected Auxiliary Space Complexity:
O(N)
, due to storing the inorder traversal.
Code (C++)
class Solution {
public:
bool findTarget(Node* root, int target) {
vector<int> inorder;
function<void(Node*)> traverse = [&](Node* node) {
if (!node) return;
traverse(node->left);
inorder.push_back(node->data);
traverse(node->right);
};
traverse(root);
int left = 0, right = inorder.size() - 1;
while (left < right) {
int sum = inorder[left] + inorder[right];
if (sum == target) return true;
sum < target ? left++ : right--;
}
return false;
}
};
Code (Java)
class Solution {
public boolean findTarget(Node root, int target) {
List<Integer> inorder = new ArrayList<>();
inorderTraversal(root, inorder);
int left = 0, right = inorder.size() - 1;
while (left < right) {
int sum = inorder.get(left) + inorder.get(right);
if (sum == target) return true;
if (sum < target) left++;
else right--;
}
return false;
}
private void inorderTraversal(Node node, List<Integer> inorder) {
if (node == null) return;
inorderTraversal(node.left, inorder);
inorder.add(node.data);
inorderTraversal(node.right, inorder);
}
}
Code (Python)
class Solution:
def findTarget(self, root, target):
inorder = []
def inorderTraversal(node):
if not node:
return
inorderTraversal(node.left)
inorder.append(node.data)
inorderTraversal(node.right)
inorderTraversal(root)
left, right = 0, len(inorder) - 1
while left < right:
total = inorder[left] + inorder[right]
if total == target:
return True
if total < target:
left += 1
else:
right -= 1
return False
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated