Given two BSTs containing n1 and n2 distinct nodes respectively. Given a value x. The problem is to count all pairs from both the BSTs whose sum is equal to x.
Input : BST 1: 5 / \ 3 7 / \ / \ 2 4 6 8 BST 2: 10 / \ 6 15 / \ / \ 3 8 11 18 x = 16 Output : 3 The pairs are: (5, 11), (6, 10) and (8, 8)
Method 1: For each node value a in BST 1, search the value (x – a) in BST 2. If value found then increment the count. For searching a value in BST, refer this post.
Time complexity: O(n1 * h2), here n1 is number of nodes in first BST and h2 is height of second BST.
Method 2: Traverse BST 1 from smallest value to node to largest. This can be achieved with the help of iterative inorder traversal. Traverse BST 2 from largest value node to smallest. This can be achieved with the help of reverse inorder traversal. Perform these two traversals simultaneously. Sum up the corresponding node’s value from both the BSTs at a particular instance of traversals. If sum == x, then increment count. If x > sum, then move to the inorder successor of the current node of BST 1, else move to the inorder predecessor of the current node of BST 2. Perform these operations until either of the two traversals gets completed.
Pairs = 3
Time Complexity: O(n1 + n2)
Auxiliary Space: O(n1 + n2)
This article is contributed by Ayush Jauhari. 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.
- Check whether the sum of element of a node is equal to the given key value
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Create new linked list from two given linked list with greater element at each node
- Count duplicates in a given linked list
- Count of ways to traverse a Matrix according to given conditions
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Find sum of all left leaves in a given Binary Tree
- Find sum of all right leaves in a given Binary Tree
- Sum of all nodes in a doubly linked list divisible by a given number K
- Find the node with minimum value in a Binary Search Tree using recursion
- Find maximum GCD value from root to leaf in a Binary tree
- Create a linked list from two linked lists by choosing max element at each position
- Program to print Sum of even and odd elements in an array
- Sum of nodes on the longest path from root to leaf node
- Sudo Placement[1.4] | K Sum
- Sum of the minimum elements in all connected components of an undirected graph
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Sum of the nodes of a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Find column with maximum sum in a Matrix
Improved By : shrikanth13