Given two Binary Search Trees (BST) and a given sum. The task is to find pairs with given sum such that each pair elements must lie in different BST.
Input : sum = 10 8 5 / \ / \ 3 10 2 18 / \ \ / \ 1 6 14 1 3 / \ / \ 5 7 13 4 Output : (5,5), (6,4), (7,3), (8,2) In above pairs first element lies in first BST and second element lies in second BST
A simple solution for this problem is to store Inorder traversal of one tree in auxiliary array then pick element one by one from the array and find its pair in other tree for given sum. Time complexity for this approach will be O(n2) if total nodes in both the trees are equal.
An efficient solution for this solution is to store Inorder traversals of both BSTs in two different auxiliary arrays vect1 and vect2. Now we follow method1 of this article. Since Inorder traversal of BST is always gives sorted sequence, we don not need to sort our arrays.
- Take iterator left and points it to the left corner vect1.
- Take iterator right and points it to the right corner vect2.
- Now if vect11[left] + vect2[right] < sum then move left iterator in vect1 in forward direction i.e; left++.
- Now if vect1[left] + vect2[right] > sum then move right iterator in vect in backward direction i.e; right–.
Below is implementation of above idea.
Time complexity : O(n)
Auxiliary space : O(n)
Time complexity : O(n)
Auxiliary Space : O(1)
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the node with minimum value in a Binary Search Tree
- Find the largest BST subtree in a given Binary Tree | Set 1
- Find k-th smallest element in BST (Order Statistics in BST)
- Merge two BSTs with limited extra space
- Find if there is a triplet in a Balanced BST that adds to zero
- Find a pair with given sum in a Balanced BST
- Check for Identical BSTs without building the trees
- Given n appointments, find all conflicting appointments
- Construct all possible BSTs for keys 1 to N
- Find the closest element in Binary Search Tree
- Sum of k smallest elements in BST
- Find median of BST in O(n) time and O(1) space
- Count pairs from two BSTs whose sum is equal to a given value x
- Check if two BSTs contain same set of elements
- Find a pair with given sum in BST
- Split a BST into two balanced BSTs based on a value K
- Sum of k largest elements in BST
- Find the closest element in Binary Search Tree | Space Efficient Method
- Find postorder traversal of BST from preorder traversal
- Number of elements smaller than root using preorder traversal of a BST