Introduction
This blog will discuss the solution to the problem to print all pairs from two BSTs whose sum is greater than the given value where we are given two binary search trees, and we have to print all the pairs whose sum is greater than X. To understand the problem better, look at the example given below:
Bst 1:

Bst 2:

X = 18
Output:
The pairs are:
{10, 10}
{10, 13}
{10, 12}
{10, 15}
{7, 13}
{7, 12}
{7, 15}
{6, 13}
{6, 15}
{5, 13}
{5, 15}
Brute force Approach
To solve this problem, print all pairs from two BSTs whose sum is greater than the given value, we will traverse the first bst, and for every node in the first bst with value Y, we will check if a node with a value greater than (X - Y) is present in the other bst or not. If it is present, then we will print that pair.
Algorithm
- Create two binary search trees and pass their heads in one function named printPairs().
- Do preorder traversal for the first tree, and every node of the first tree check for pairs in the second tree by doing preorder in the second tree.
- If there is a pair where the sum of nodes of both the trees is greater than x, print the pair.
- After that, make two recursive calls, one for the second binary search tree's left side and another for the second binary search tree, and another for the right side of the second binary search tree.
- Now all the pairs will be printed one by one.
Code in C++
// C++ implementation to print all pairs from two BSTs whose sum is greater than the given value
#include <bits/stdc++.h>
using namespace std;
// Structure of each node of BST
struct node
{
int key;
struct node *left, *right;
};
// Function to create a new BST node
node *TreeNode(int data)
{
node *temp = new node();
temp->key = data;
temp->left = temp->right = NULL;
return temp;
}
// function to insert node in bst
struct node *insert(struct node *node,
int key)
{
// If the tree is empty, then return a new node
if (node == NULL)
return TreeNode(key);
// Otherwise, recur down the tree
if (key < node->key)
node->left = insert(node->left,key);
else if (key > node->key)
node->right = insert(node->right,key);
// Return the (unchanged) node pointer
return node;
}
void findPair(node *root1, node *root2, int x)
{
// base case
if (!root2)
return;
// following preorder root --> left --> right
// check if the current node values of both the trees is greater than x or not
if (root1->key + root2->key > x)
{
// if greater then print the pair
cout << root1->key << " " << root2->key << endl;
}
// make recusive call to left side of second tree when it is not null
findPair(root1, root2->left, x);
// make recusive call to right side of second tree when it is not null
findPair(root1, root2->right, x);
return;
}
void printPairs(node *root1, node *root2, int x)
{
// base case
if (!root1)
return;
// following preorder root --> left --> right
// check for pairs with current node of first tree
findPair(root1, root2, x);
// make recursive call to the left side of first tree when it is not null
printPairs(root1->left, root2, x);
// make recursive call to the right side of first tree when it is not null
printPairs(root1->right, root2, x);
return;
}
int main()
{
/* Formation of BST 1
5
/ \
3 7
/\ /\
2 4 6 8
*/
struct node *root1 = NULL;
root1 = insert(root1, 5);
insert(root1, 3);
insert(root1, 2);
insert(root1, 4);
insert(root1, 7);
insert(root1, 6);
insert(root1, 8);
/* Formation of BST 2
4
/ \
2 6
/\ /\
1 3 5 7
*/
struct node *root2 = NULL;
root2 = insert(root2, 4);
insert(root2, 2);
insert(root2, 1);
insert(root2, 3);
insert(root2, 6);
insert(root2, 5);
insert(root2, 7);
int x = 10;
// Print pairs
printPairs(root1, root2, x);
return 0;
}
Output:
5 6
5 7
4 7
7 4
7 6
7 5
7 7
6 6
6 5
6 7
8 4
8 3
8 6
8 5
8 7
Complexity Analysis
Time Complexity: O(n1*h2)
Here n1 is the number of nodes in the first bst, and h2 is the height of the second tree, and since we are traversing the second tree for every node of the first tree, the time complexity is O(n1*h2).
Space Complexity: O(1)
Since no extra space is used, the space complexity will be O(1).





