Find the minimum absolute difference in two different BST’s
Given 2 Binary Search Trees, select one node from each tree such that their absolute difference is minimum possible. Assume each BST has at-least one node.
Examples:
Input : N1 = 7, N2 = 2
BST1 :
5
/ \
3 7
/ \ / \
2 4 6 8
BST2 :
11
\
13
Output : 3
8 is largest number in the first BST
and 11 is smallest in the second.
Thus, the final answer will be 11-8 = 3
Input : N1 = 4, N2 = 2
BST1 :
3
/ \
2 4
\
14
BST2 :
7
\
13
Output : 1
Approach:
The idea is to use the two-pointer technique and iterating the pointers using the following steps.
- Create forward iterators for both the BST’s. Let’s say that the value of nodes they are pointing at are v1 and v2 respectively.
- Now at each step:
- Update final ans as min(ans, abs(v1-v2)) .
- If v1 < v2, move iterator of first BST else move the iterator of the second BST.
- Repeat above steps till both the BST’s are pointing to a valid nodes.
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Node of Binary tree struct node { int data; node* left; node* right; node(int data) { this->data = data; left = NULL; right = NULL; } }; // Function to iterate to the // next element of the BST void next(stack<node*>& it) { node* curr = it.top()->right; it.pop(); while (curr != NULL) it.push(curr), curr = curr->left; } // Function to find minimum difference int minDiff(node* root1, node* root2) { // Iterator for two Binary Search Trees stack<node *> it1, it2; // Initializing first iterator node* curr = root1; while (curr != NULL) it1.push(curr), curr = curr->left; // Initializing second iterator curr = root2; while (curr != NULL) it2.push(curr), curr = curr->left; // Variable to store final answer int ans = INT_MAX; // Two pointer technique while (it1.size() and it2.size()) { // value it1 and it2 are pointing to int v1 = it1.top()->data; int v2 = it2.top()->data; // Updating final answer ans = min(abs(v1 - v2), ans); // Case when v1 < v2 if (v1 < v2) next(it1); else next(it2); } // Return ans return ans; } // Driver code int main() { // BST-1 /* 5 / \ 3 7 / \ / \ 2 4 6 8 */ node* root2 = new node(5); root2->left = new node(3); root2->right = new node(7); root2->left->left = new node(2); root2->left->right = new node(4); root2->right->left = new node(6); root2->right->right = new node(8); // BST-2 /* 11 \ 15 */ node* root1 = new node(11); root1->right = new node(15); cout << minDiff(root1, root2); return 0; } |
3
Time complexity: O(N1 + N2) where N1 and N2 are the bumber of nodes of first and second BST respectively.
Space Complexity: O(H1 + H2) where H1 and H2 are the heights of first and second BST respectively.
Recommended Posts:
- Find the node whose absolute difference with X gives minimum value
- Pair with minimum absolute difference | BST
- Pair with minimum absolute difference after solving each query
- Find the node whose absolute difference with X gives maximum value
- Sort an array according to absolute difference with given value using Functors
- Queries to add, remove and return the difference of maximum and minimum.
- Sub-tree with minimum color difference in a 2-coloured tree
- Find the node whose xor with x gives minimum value
- Find a number in minimum steps
- Find the node whose sum with X has minimum set bits
- Find maximum (or minimum) in Binary Tree
- Find Minimum Depth of a Binary Tree
- Find the root of the sub-tree whose weighted sum XOR with X is minimum
- Find the weight of the minimum spanning tree
- Find the root of the sub-tree whose weighted sum is minimum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



