Open In App

Find pairs with given sum such that pair elements lie in different BSTs

Last Updated : 11 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Examples: 

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. 

C++




// C++ program to find pairs with given sum such
// that one element of pair exists in one BST and
// other in other BST.
#include<bits/stdc++.h>
using namespace std;
  
// A binary Tree node
struct Node
{
    int data;
    struct Node *left, *right;
};
  
// A utility function to create a new BST node
// with key as given num
struct Node* newNode(int num)
{
    struct Node* temp = new Node;
    temp->data = num;
    temp->left = temp->right = NULL;
    return temp;
}
  
// A utility function to insert a given key to BST
Node* insert(Node* root, int key)
{
    if (root == NULL)
        return newNode(key);
    if (root->data > key)
        root->left = insert(root->left, key);
    else
        root->right = insert(root->right, key);
    return root;
}
  
// store storeInorder traversal in auxiliary array
void storeInorder(Node *ptr, vector<int> &vect)
{
    if (ptr==NULL)
        return;
    storeInorder(ptr->left, vect);
    vect.push_back(ptr->data);
    storeInorder(ptr->right, vect);
}
  
// Function to find pair for given sum in different bst
// vect1[]  --> stores storeInorder traversal of first bst
// vect2[]  --> stores storeInorder traversal of second bst
void pairSumUtil(vector<int> &vect1, vector<int> &vect2,
                                                int sum)
{
    // Initialize two indexes to two different corners
    // of two vectors.
    int left = 0;
    int right = vect2.size() - 1;
  
    // find pair by moving two corners.
    while (left < vect1.size() && right >= 0)
    {
        // If we found a pair
        if (vect1[left] + vect2[right] == sum)
        {
            cout << "(" << vect1[left] << ", "
                 << vect2[right] << "), ";
            left++;
            right--;
        }
  
        // If sum is more, move to higher value in
        // first vector.
        else if (vect1[left] + vect2[right] < sum)
            left++;
  
        // If sum is less, move to lower value in
        // second vector.
        else
            right--;
    }
}
  
// Prints all pairs with given "sum" such that one
// element of pair is in tree with root1 and other
// node is in tree with root2.
void pairSum(Node *root1, Node *root2, int sum)
{
    // Store inorder traversals of two BSTs in two
    // vectors.
    vector<int> vect1, vect2;
    storeInorder(root1, vect1);
    storeInorder(root2, vect2);
  
    // Now the problem reduces to finding a pair
    // with given sum such that one element is in
    // vect1 and other is in vect2.
    pairSumUtil(vect1, vect2, sum);
}
  
// Driver program to run the case
int main()
{
    // first BST
    struct Node* root1 = NULL;
    root1 = insert(root1, 8);
    root1 = insert(root1, 10);
    root1 = insert(root1, 3);
    root1 = insert(root1, 6);
    root1 = insert(root1, 1);
    root1 = insert(root1, 5);
    root1 = insert(root1, 7);
    root1 = insert(root1, 14);
    root1 = insert(root1, 13);
  
    // second BST
    struct Node* root2 = NULL;
    root2 = insert(root2, 5);
    root2 = insert(root2, 18);
    root2 = insert(root2, 2);
    root2 = insert(root2, 1);
    root2 = insert(root2, 3);
    root2 = insert(root2, 4);
  
    int sum = 10;
    pairSum(root1, root2, sum);
  
    return 0;
}


Java




// Java program to find pairs with given sum such
// that one element of pair exists in one BST and
// other in other BST.
import java.util.*;
class solution
{
   
// A binary Tree node
static class Node
{
    int data;
     Node left, right;
};
   
// A utility function to create a new BST node
// with key as given num
static Node newNode(int num)
{
     Node temp = new Node();
    temp.data = num;
    temp.left = temp.right = null;
    return temp;
}
   
// A utility function to insert a given key to BST
static Node insert(Node root, int key)
{
    if (root == null)
        return newNode(key);
    if (root.data > key)
        root.left = insert(root.left, key);
    else
        root.right = insert(root.right, key);
    return root;
}
   
// store storeInorder traversal in auxiliary array
static void storeInorder(Node ptr, Vector<Integer> vect)
{
    if (ptr==null)
        return;
    storeInorder(ptr.left, vect);
    vect.add(ptr.data);
    storeInorder(ptr.right, vect);
}
   
// Function to find pair for given sum in different bst
// vect1.get()  -. stores storeInorder traversal of first bst
// vect2.get()  -. stores storeInorder traversal of second bst
static void pairSumUtil(Vector<Integer> vect1, Vector<Integer> vect2,
                                                int sum)
{
    // Initialize two indexes to two different corners
    // of two Vectors.
    int left = 0;
    int right = vect2.size() - 1;
   
    // find pair by moving two corners.
    while (left < vect1.size() && right >= 0)
    {
        // If we found a pair
        if (vect1.get(left) + vect2.get(right) == sum)
        {
            System.out.print( "(" +vect1.get(left) + ", "+ vect2.get(right) + "), ");
            left++;
            right--;
        }
   
        // If sum is more, move to higher value in
        // first Vector.
        else if (vect1.get(left) + vect2.get(right) < sum)
            left++;
   
        // If sum is less, move to lower value in
        // second Vector.
        else
            right--;
    }
}
   
// Prints all pairs with given "sum" such that one
// element of pair is in tree with root1 and other
// node is in tree with root2.
static void pairSum(Node root1, Node root2, int sum)
{
    // Store inorder traversals of two BSTs in two
    // Vectors.
    Vector<Integer> vect1= new Vector<Integer>(), vect2= new Vector<Integer>();
    storeInorder(root1, vect1);
    storeInorder(root2, vect2);
   
    // Now the problem reduces to finding a pair
    // with given sum such that one element is in
    // vect1 and other is in vect2.
    pairSumUtil(vect1, vect2, sum);
}
   
// Driver program to run the case
public static void  main(String args[])
{
    // first BST
     Node root1 = null;
    root1 = insert(root1, 8);
    root1 = insert(root1, 10);
    root1 = insert(root1, 3);
    root1 = insert(root1, 6);
    root1 = insert(root1, 1);
    root1 = insert(root1, 5);
    root1 = insert(root1, 7);
    root1 = insert(root1, 14);
    root1 = insert(root1, 13);
   
    // second BST
     Node root2 = null;
    root2 = insert(root2, 5);
    root2 = insert(root2, 18);
    root2 = insert(root2, 2);
    root2 = insert(root2, 1);
    root2 = insert(root2, 3);
    root2 = insert(root2, 4);
   
    int sum = 10;
    pairSum(root1, root2, sum);
}
}
//contributed by Arnab Kundu


Python3




# Python3 program to find pairs with given
# sum such that one element of pair exists 
# in one BST and other in other BST. 
  
# A utility function to create a new 
# BST node with key as given num 
class newNode: 
  
    # Constructor to create a new node 
    def __init__(self, data): 
        self.data = data 
        self.left = None
        self.right = None
  
# A utility function to insert a 
# given key to BST 
def insert(root, key):
    if root == None
        return newNode(key) 
    if root.data > key: 
        root.left = insert(root.left, key) 
    else:
        root.right = insert(root.right, key) 
    return root
  
# store storeInorder traversal in
# auxiliary array 
def storeInorder(ptr, vect):
    if ptr == None:
        return
    storeInorder(ptr.left, vect) 
    vect.append(ptr.data) 
    storeInorder(ptr.right, vect)
  
# Function to find pair for given
# sum in different bst 
# vect1[] --> stores storeInorder traversal
#             of first bst 
# vect2[] --> stores storeInorder traversal 
#             of second bst 
def pairSumUtil(vect1, vect2, Sum):
      
    # Initialize two indexes to two 
    # different corners of two lists. 
    left = 0
    right = len(vect2) - 1
  
    # find pair by moving two corners. 
    while left < len(vect1) and right >= 0:
          
        # If we found a pair 
        if vect1[left] + vect2[right] == Sum:
            print("(", vect1[left], ","
                       vect2[right], "),", end = " "
            left += 1
            right -= 1
  
        # If sum is more, move to higher 
        # value in first lists. 
        else if vect1[left] + vect2[right] < Sum
            left += 1
  
        # If sum is less, move to lower 
        # value in second lists. 
        else:
            right -= 1
  
# Prints all pairs with given "sum" such that one 
# element of pair is in tree with root1 and other 
# node is in tree with root2. 
def pairSum(root1, root2, Sum):
      
    # Store inorder traversals of 
    # two BSTs in two lists. 
    vect1 = []
    vect2 = []
    storeInorder(root1, vect1) 
    storeInorder(root2, vect2) 
  
    # Now the problem reduces to finding a 
    # pair with given sum such that one  
    # element is in vect1 and other is in vect2. 
    pairSumUtil(vect1, vect2, Sum)
  
# Driver Code
if __name__ == '__main__':
      
    # first BST 
    root1 = None
    root1 = insert(root1, 8
    root1 = insert(root1, 10)
    root1 = insert(root1, 3
    root1 = insert(root1, 6
    root1 = insert(root1, 1
    root1 = insert(root1, 5
    root1 = insert(root1, 7
    root1 = insert(root1, 14
    root1 = insert(root1, 13)
  
    # second BST 
    root2 = None
    root2 = insert(root2, 5
    root2 = insert(root2, 18
    root2 = insert(root2, 2
    root2 = insert(root2, 1
    root2 = insert(root2, 3
    root2 = insert(root2, 4
  
    Sum = 10
    pairSum(root1, root2, Sum)
  
# This code is contributed by PranchalK


C#




using System;
using System.Collections.Generic;
  
// C# program to find pairs with given sum such 
// that one element of pair exists in one BST and 
// other in other BST. 
public class solution
{
  
// A binary Tree node 
public class Node
{
    public int data;
     public Node left, right;
}
  
// A utility function to create a new BST node 
// with key as given num 
public static Node newNode(int num)
{
     Node temp = new Node();
    temp.data = num;
    temp.left = temp.right = null;
    return temp;
}
  
// A utility function to insert a given key to BST 
public static Node insert(Node root, int key)
{
    if (root == null)
    {
        return newNode(key);
    }
    if (root.data > key)
    {
        root.left = insert(root.left, key);
    }
    else
    {
        root.right = insert(root.right, key);
    }
    return root;
}
  
// store storeInorder traversal in auxiliary array 
public static void storeInorder(Node ptr, List<int> vect)
{
    if (ptr == null)
    {
        return;
    }
    storeInorder(ptr.left, vect);
    vect.Add(ptr.data);
    storeInorder(ptr.right, vect);
}
  
// Function to find pair for given sum in different bst 
// vect1.get()  -. stores storeInorder traversal of first bst 
// vect2.get()  -. stores storeInorder traversal of second bst 
public static void pairSumUtil(List<int> vect1, List<int> vect2, int sum)
{
    // Initialize two indexes to two different corners 
    // of two Vectors. 
    int left = 0;
    int right = vect2.Count - 1;
  
    // find pair by moving two corners. 
    while (left < vect1.Count && right >= 0)
    {
        // If we found a pair 
        if (vect1[left] + vect2[right] == sum)
        {
            Console.Write("(" + vect1[left] + ", " + vect2[right] + "), ");
            left++;
            right--;
        }
  
        // If sum is more, move to higher value in 
        // first Vector. 
        else if (vect1[left] + vect2[right] < sum)
        {
            left++;
        }
  
        // If sum is less, move to lower value in 
        // second Vector. 
        else
        {
            right--;
        }
    }
}
  
// Prints all pairs with given "sum" such that one 
// element of pair is in tree with root1 and other 
// node is in tree with root2. 
public static void pairSum(Node root1, Node root2, int sum)
{
    // Store inorder traversals of two BSTs in two 
    // Vectors. 
    List<int> vect1 = new List<int>(), vect2 = new List<int>();
    storeInorder(root1, vect1);
    storeInorder(root2, vect2);
  
    // Now the problem reduces to finding a pair 
    // with given sum such that one element is in 
    // vect1 and other is in vect2. 
    pairSumUtil(vect1, vect2, sum);
}
  
// Driver program to run the case 
public static void Main(string[] args)
{
    // first BST 
     Node root1 = null;
    root1 = insert(root1, 8);
    root1 = insert(root1, 10);
    root1 = insert(root1, 3);
    root1 = insert(root1, 6);
    root1 = insert(root1, 1);
    root1 = insert(root1, 5);
    root1 = insert(root1, 7);
    root1 = insert(root1, 14);
    root1 = insert(root1, 13);
  
    // second BST 
     Node root2 = null;
    root2 = insert(root2, 5);
    root2 = insert(root2, 18);
    root2 = insert(root2, 2);
    root2 = insert(root2, 1);
    root2 = insert(root2, 3);
    root2 = insert(root2, 4);
  
    int sum = 10;
    pairSum(root1, root2, sum);
}
}
  
  // This code is contributed by Shrikant13


Javascript




<script>
  
// JavaScript program to find pairs with given sum such 
// that one element of pair exists in one BST and 
// other in other BST. 
  
// A binary Tree node 
class Node
{
    constructor()
    {
        this.data = 0;
        this.left = null;
        this.right = null;
    }
}
  
// A utility function to create a new BST node 
// with key as given num 
function newNode(num)
{
     var temp = new Node();
    temp.data = num;
    temp.left = temp.right = null;
    return temp;
}
  
// A utility function to insert a given key to BST 
function insert(root, key)
{
    if (root == null)
    {
        return newNode(key);
    }
    if (root.data > key)
    {
        root.left = insert(root.left, key);
    }
    else
    {
        root.right = insert(root.right, key);
    }
    return root;
}
  
// store storeInorder traversal in auxiliary array 
function storeInorder(ptr, vect)
{
    if (ptr == null)
    {
        return;
    }
    storeInorder(ptr.left, vect);
    vect.push(ptr.data);
    storeInorder(ptr.right, vect);
}
  
// Function to find pair for given sum in different bst 
// vect1.get()  -. stores storeInorder traversal of first bst 
// vect2.get()  -. stores storeInorder traversal of second bst 
function pairSumUtil(vect1, vect2, sum)
{
    // Initialize two indexes to two different corners 
    // of two Vectors. 
    var left = 0;
    var right = vect2.length - 1;
  
    // find pair by moving two corners. 
    while (left < vect1.length && right >= 0)
    {
        // If we found a pair 
        if (vect1[left] + vect2[right] == sum)
        {
            document.write("(" + vect1[left] + ", "
            vect2[right] + "), ");
            left++;
            right--;
        }
  
        // If sum is more, move to higher value in 
        // first Vector. 
        else if (vect1[left] + vect2[right] < sum)
        {
            left++;
        }
  
        // If sum is less, move to lower value in 
        // second Vector. 
        else
        {
            right--;
        }
    }
}
  
// Prints all pairs with given "sum" such that one 
// element of pair is in tree with root1 and other 
// node is in tree with root2. 
function pairSum(root1, root2, sum)
{
    // Store inorder traversals of two BSTs in two 
    // Vectors. 
    var vect1 = [], vect2 = [];
    storeInorder(root1, vect1);
    storeInorder(root2, vect2);
  
    // Now the problem reduces to finding a pair 
    // with given sum such that one element is in 
    // vect1 and other is in vect2. 
    pairSumUtil(vect1, vect2, sum);
}
  
// Driver program to run the case 
// first BST 
 var root1 = null;
root1 = insert(root1, 8);
root1 = insert(root1, 10);
root1 = insert(root1, 3);
root1 = insert(root1, 6);
root1 = insert(root1, 1);
root1 = insert(root1, 5);
root1 = insert(root1, 7);
root1 = insert(root1, 14);
root1 = insert(root1, 13);
// second BST 
 var root2 = null;
root2 = insert(root2, 5);
root2 = insert(root2, 18);
root2 = insert(root2, 2);
root2 = insert(root2, 1);
root2 = insert(root2, 3);
root2 = insert(root2, 4);
var sum = 10;
pairSum(root1, root2, sum);
  
</script>


Output

(5, 5), (6, 4), (7, 3), (8, 2), 

Time complexity : O(n) 
Auxiliary space : O(n)

We have another space optimized approach to solve this problem. The idea is to convert bst into doubly linked list and apply above method for doubly linked list.See this article.

Time complexity : O(n) 
Auxiliary Space : O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads