Merge two BSTs with limited extra space
Given two Binary Search Trees(BST), print the elements of both BSTs in sorted form. The expected time complexity is O(m+n) where m is the number of nodes in the first tree and n is the number of nodes in the second tree. The maximum allowed auxiliary space is O(height of the first tree + height of the second tree).
Examples:
First BST
3
/ \
1 5
Second BST
4
/ \
2 6
Output: 1 2 3 4 5 6
First BST
8
/ \
2 10
/
1
Second BST
5
/
3
/
0
Output: 0 1 2 3 5 8 10 Source: Google interview question
A similar question has been discussed earlier. Let us first discuss the already discussed methods of the previous post which was for Balanced BSTs. Method 1 can be applied here also, but the time complexity will be O(n^2) in the worst case. Method 2 can also be applied here, but the extra space required will be O(n) which violates the constraint given in this question. Method 3 can be applied here but step 3 of method 3 can’t be done in O(n) for an unbalanced BST.
Thanks to Kumar for suggesting the following solution.
The idea is to use iterative inorder traversal. We use two auxiliary stacks for two BSTs. Since we need to print the elements in the sorted form, whenever we get a smaller element from any of the trees, we print it. If the element is greater, then we push it back to stack for the next iteration.
C++
#include <bits/stdc++.h>using namespace std;// Structure of a BST Nodeclass node{ public: int data; node *left; node *right;};//.................... START OF STACK RELATED STUFF....................// A stack nodeclass snode{ public: node *t; snode *next;};// Function to add an element k to stackvoid push(snode **s, node *k){ snode *tmp = new snode(); //perform memory check here tmp->t = k; tmp->next = *s; (*s) = tmp;}// Function to pop an element t from stacknode *pop(snode **s){ node *t; snode *st; st=*s; (*s) = (*s)->next; t = st->t; free(st); return t;}// Function to check whether the stack is empty or notint isEmpty(snode *s){ if (s == NULL ) return 1; return 0;}//.................... END OF STACK RELATED STUFF..................../* Utility function to create a new Binary Tree node */node* newNode (int data){ node *temp = new node; temp->data = data; temp->left = NULL; temp->right = NULL; return temp;}/* A utility function to print Inorder traversal of a Binary Tree */void inorder(node *root){ if (root != NULL) { inorder(root->left); cout<<root->data<<" "; inorder(root->right); }}// The function to print data of two BSTs in sorted ordervoid merge(node *root1, node *root2){ // s1 is stack to hold nodes of first BST snode *s1 = NULL; // Current node of first BST node *current1 = root1; // s2 is stack to hold nodes of second BST snode *s2 = NULL; // Current node of second BST node *current2 = root2; // If first BST is empty, then output is inorder // traversal of second BST if (root1 == NULL) { inorder(root2); return; } // If second BST is empty, then output is inorder // traversal of first BST if (root2 == NULL) { inorder(root1); return ; } // Run the loop while there are nodes not yet printed. // The nodes may be in stack(explored, but not printed) // or may be not yet explored while (current1 != NULL || !isEmpty(s1) || current2 != NULL || !isEmpty(s2)) { // Following steps follow iterative Inorder Traversal if (current1 != NULL || current2 != NULL ) { // Reach the leftmost node of both BSTs and push ancestors of // leftmost nodes to stack s1 and s2 respectively if (current1 != NULL) { push(&s1;, current1); current1 = current1->left; } if (current2 != NULL) { push(&s2;, current2); current2 = current2->left; } } else { // If we reach a NULL node and either of the stacks is empty, // then one tree is exhausted, print the other tree if (isEmpty(s1)) { while (!isEmpty(s2)) { current2 = pop (&s2;); current2->left = NULL; inorder(current2); } return ; } if (isEmpty(s2)) { while (!isEmpty(s1)) { current1 = pop (&s1;); current1->left = NULL; inorder(current1); } return ; } // Pop an element from both stacks and compare the // popped elements current1 = pop(&s1;); current2 = pop(&s2;); // If element of first tree is smaller, then print it // and push the right subtree. If the element is larger, // then we push it back to the corresponding stack. if (current1->data < current2->data) { cout<<current1->data<<" "; current1 = current1->right; push(&s2;, current2); current2 = NULL; } else { cout<<current2->data<<" "; current2 = current2->right; push(&s1;, current1); current1 = NULL; } } }}/* Driver program to test above functions */int main(){ node *root1 = NULL, *root2 = NULL; /* Let us create the following tree as first tree 3 / \ 1 5 */ root1 = newNode(3); root1->left = newNode(1); root1->right = newNode(5); /* Let us create the following tree as second tree 4 / \ 2 6 */ root2 = newNode(4); root2->left = newNode(2); root2->right = newNode(6); // Print sorted nodes of both trees merge(root1, root2); return 0;}//This code is contributed by rathbhupendra |
C
#include<stdio.h>#include<stdlib.h>// Structure of a BST Nodestruct node{ int data; struct node *left; struct node *right;};//.................... START OF STACK RELATED STUFF....................// A stack nodestruct snode{ struct node *t; struct snode *next;};// Function to add an element k to stackvoid push(struct snode **s, struct node *k){ struct snode *tmp = (struct snode *) malloc(sizeof(struct snode)); //perform memory check here tmp->t = k; tmp->next = *s; (*s) = tmp;}// Function to pop an element t from stackstruct node *pop(struct snode **s){ struct node *t; struct snode *st; st=*s; (*s) = (*s)->next; t = st->t; free(st); return t;}// Function to check whether the stack is empty or notint isEmpty(struct snode *s){ if (s == NULL ) return 1; return 0;}//.................... END OF STACK RELATED STUFF..................../* Utility function to create a new Binary Tree node */struct node* newNode (int data){ struct node *temp = (struct node*)malloc(sizeof(struct node)); temp->data = data; temp->left = NULL; temp->right = NULL; return temp;}/* A utility function to print Inorder traversal of a Binary Tree */void inorder(struct node *root){ if (root != NULL) { inorder(root->left); printf("%d ", root->data); inorder(root->right); }}// The function to print data of two BSTs in sorted ordervoid merge(struct node *root1, struct node *root2){ // s1 is stack to hold nodes of first BST struct snode *s1 = NULL; // Current node of first BST struct node *current1 = root1; // s2 is stack to hold nodes of second BST struct snode *s2 = NULL; // Current node of second BST struct node *current2 = root2; // If first BST is empty, then output is inorder // traversal of second BST if (root1 == NULL) { inorder(root2); return; } // If second BST is empty, then output is inorder // traversal of first BST if (root2 == NULL) { inorder(root1); return ; } // Run the loop while there are nodes not yet printed. // The nodes may be in stack(explored, but not printed) // or may be not yet explored while (current1 != NULL || !isEmpty(s1) || current2 != NULL || !isEmpty(s2)) { // Following steps follow iterative Inorder Traversal if (current1 != NULL || current2 != NULL ) { // Reach the leftmost node of both BSTs and push ancestors of // leftmost nodes to stack s1 and s2 respectively if (current1 != NULL) { push(&s1;, current1); current1 = current1->left; } if (current2 != NULL) { push(&s2;, current2); current2 = current2->left; } } else { // If we reach a NULL node and either of the stacks is empty, // then one tree is exhausted, print the other tree if (isEmpty(s1)) { while (!isEmpty(s2)) { current2 = pop (&s2;); current2->left = NULL; inorder(current2); } return ; } if (isEmpty(s2)) { while (!isEmpty(s1)) { current1 = pop (&s1;); current1->left = NULL; inorder(current1); } return ; } // Pop an element from both stacks and compare the // popped elements current1 = pop(&s1;); current2 = pop(&s2;); // If element of first tree is smaller, then print it // and push the right subtree. If the element is larger, // then we push it back to the corresponding stack. if (current1->data < current2->data) { printf("%d ", current1->data); current1 = current1->right; push(&s2;, current2); current2 = NULL; } else { printf("%d ", current2->data); current2 = current2->right; push(&s1;, current1); current1 = NULL; } } }}/* Driver program to test above functions */int main(){ struct node *root1 = NULL, *root2 = NULL; /* Let us create the following tree as first tree 3 / \ 1 5 */ root1 = newNode(3); root1->left = newNode(1); root1->right = newNode(5); /* Let us create the following tree as second tree 4 / \ 2 6 */ root2 = newNode(4); root2->left = newNode(2); root2->right = newNode(6); // Print sorted nodes of both trees merge(root1, root2); return 0;} |
Java
public class Merge2BST{ /* A utility function to print Inorder traversal of a Binary Tree */ static void inorder(Node root) { if (root != null) { inorder(root.left); System.out.print(root.data + " "); inorder(root.right); } } // The function to print data of two BSTs in sorted order static void merge(Node root1, Node root2) { // s1 is stack to hold nodes of first BST SNode s1 = new SNode(); // Current node of first BST Node current1 = root1; // s2 is stack to hold nodes of second BST SNode s2 = new SNode(); // Current node of second BST Node current2 = root2; // If first BST is empty, then output is inorder // traversal of second BST if (root1 == null) { inorder(root2); return; } // If second BST is empty, then output is inorder // traversal of first BST if (root2 == null) { inorder(root1); return ; } // Run the loop while there are nodes not yet printed. // The nodes may be in stack(explored, but not printed) // or may be not yet explored while (current1 != null || !s1.isEmpty() || current2 != null || !s2.isEmpty()) { // Following steps follow iterative Inorder Traversal if (current1 != null || current2 != null ) { // Reach the leftmost node of both BSTs and push ancestors of // leftmost nodes to stack s1 and s2 respectively if (current1 != null) { s1.push( current1); current1 = current1.left; } if (current2 != null) { s2.push( current2); current2 = current2.left; } } else { // If we reach a NULL node and either of the stacks is empty, // then one tree is exhausted, print the other tree if (s1.isEmpty()) { while (!s2.isEmpty()) { current2 = s2.pop (); current2.left = null; inorder(current2); } return ; } if (s2.isEmpty()) { while (!s1.isEmpty()) { current1 = s1.pop (); current1.left = null; inorder(current1); } return ; } // Pop an element from both stacks and compare the // popped elements current1 = s1.pop(); current2 = s2.pop(); // If element of first tree is smaller, then print it // and push the right subtree. If the element is larger, // then we push it back to the corresponding stack. if (current1.data < current2.data) { System.out.print(current1.data + " "); current1 = current1.right; s2.push( current2); current2 = null; } else { System.out.print(current2.data + " "); current2 = current2.right; s1.push( current1); current1 = null; } } } System.out.println(s1.t); System.out.println(s2.t); } /* Driver code */ public static void main(String[]args) { Node root1 = null, root2 = null; /* Let us create the following tree as first tree 3 / \ 1 5 */ root1 = new Node(3) ; root1.left = new Node(1); root1.right = new Node(5); /* Let us create the following tree as second tree 4 / \ 2 6 */ root2 = new Node(4) ; root2.left = new Node(2); root2.right = new Node(6); // Print sorted nodes of both trees merge(root1, root2); }}// Structure of a BST Nodeclass Node{ int data; Node left; Node right; public Node(int data) { // TODO Auto-generated constructor stub this.data = data; this.left = null; this.right = null; }}// A stack nodeclass SNode{ SNode head; Node t; SNode next; // Function to add an element k to stack void push(Node k) { SNode tmp = new SNode(); // Perform memory check here tmp.t = k; tmp.next = this.head; this.head = tmp; } // Function to pop an element t from stack Node pop() { SNode st; st = this.head; head = head.next; return st.t; } // Function to check whether the stack is empty or not boolean isEmpty( ) { if (this.head == null ) return true; return false; }}// This code is contributed by nidhisebastian008 |
Python 3
# Class to create a new Tree Nodeclass newNode: def __init__(self, data: int): self.data = data self.left = None self.right = Nonedef inorder(root: newNode): if root: inorder(root.left) print(root.data, end=" ") inorder(root.right)def merge(root1: newNode, root2: newNode): # s1 is stack to hold nodes of first BST s1 = [] # Current node of first BST current1 = root1 # s2 is stack to hold nodes of first BST s2 = [] # Current node of second BST current2 = root2 # If first BST is empty then the output is the # inorder traversal of the second BST if not root1: return inorder(root2) # If the second BST is empty then the output is the # inorder traversal of the first BST if not root2: return inorder(root1) # Run the loop while there are nodes not yet printed. # The nodes may be in stack(explored, but not printed) # or may be not yet explored while current1 or s1 or current2 or s2: # Following steps follow iterative Inorder Traversal if current1 or current2: # Reach the leftmost node of both BSTs and push ancestors of # leftmost nodes to stack s1 and s2 respectively if current1: s1.append(current1) current1 = current1.left if current2: s2.append(current2) current2 = current2.left else: # If we reach a NULL node and either of the stacks is empty, # then one tree is exhausted, print the other tree if not s1: while s2: current2 = s2.pop() current2.left = None inorder(current2) return if not s2: while s1: current1 = s1.pop() current1.left = None inorder(current1) return # Pop an element from both stacks and compare the # popped elements current1 = s1.pop() current2 = s2.pop() # If element of first tree is smaller, then print it # and push the right subtree. If the element is larger, # then we push it back to the corresponding stack. if current1.data < current2.data: print(current1.data, end=" ") current1 = current1.right s2.append(current2) current2 = None else: print(current2.data, end=" ") current2 = current2.right s1.append(current1) current1 = None# Driver codedef main(): # Let us create the following tree as first tree # 3 # / \ # 1 5 root1 = newNode(3) root1.left = newNode(1) root1.right = newNode(5) # Let us create the following tree as second tree # 4 # / \ # 2 6 # root2 = newNode(4) root2.left = newNode(2) root2.right = newNode(6) merge(root1, root2)if __name__ == "__main__": main()# This code is contributed by Koushik Reddy Bukkasamudram |
C#
// C# program to implement the// above approachusing System;class Merge2BST{ /* A utility function to print Inorder traversal of a Binary Tree */static void inorder(Node root){ if (root != null) { inorder(root.left); Console.Write(root.data + " "); inorder(root.right); }}// The function to print data// of two BSTs in sorted orderstatic void merge(Node root1, Node root2){ // s1 is stack to hold nodes // of first BST SNode s1 = new SNode(); // Current node of first BST Node current1 = root1; // s2 is stack to hold nodes // of second BST SNode s2 = new SNode(); // Current node of second BST Node current2 = root2; // If first BST is empty, then // output is inorder traversal // of second BST if (root1 == null) { inorder(root2); return; } // If second BST is empty, // then output is inorder // traversal of first BST if (root2 == null) { inorder(root1); return ; } // Run the loop while there // are nodes not yet printed. // The nodes may be in stack // (explored, but not printed) // or may be not yet explored while (current1 != null || !s1.isEmpty() || current2 != null || !s2.isEmpty()) { // Following steps follow // iterative Inorder Traversal if (current1 != null || current2 != null) { // Reach the leftmost node of // both BSTs and push ancestors // of leftmost nodes to stack // s1 and s2 respectively if (current1 != null) { s1.push(current1); current1 = current1.left; } if (current2 != null) { s2.push(current2); current2 = current2.left; } } else { // If we reach a NULL node and // either of the stacks is empty, // then one tree is exhausted, // print the other tree if (s1.isEmpty()) { while (!s2.isEmpty()) { current2 = s2.pop (); current2.left = null; inorder(current2); } return; } if (s2.isEmpty()) { while (!s1.isEmpty()) { current1 = s1.pop (); current1.left = null; inorder(current1); } return; } // Pop an element from both // stacks and compare the // popped elements current1 = s1.pop(); current2 = s2.pop(); // If element of first tree is // smaller, then print it // and push the right subtree. // If the element is larger, // then we push it back to the // corresponding stack. if (current1.data < current2.data) { Console.Write(current1.data + " "); current1 = current1.right; s2.push( current2); current2 = null; } else { Console.Write(current2.data + " "); current2 = current2.right; s1.push( current1); current1 = null; } } } Console.Write(s1.t + "\n"); Console.Write(s2.t + "\n");}// Driver codepublic static void Main(string[]args){ Node root1 = null, root2 = null; /* Let us create the following tree as first tree 3 / \ 1 5 */ root1 = new Node(3) ; root1.left = new Node(1); root1.right = new Node(5); /* Let us create the following tree as second tree 4 / \ 2 6 */ root2 = new Node(4) ; root2.left = new Node(2); root2.right = new Node(6); // Print sorted nodes of // both trees merge(root1, root2);}}// Structure of a BST Nodeclass Node{ public int data;public Node left;public Node right; public Node(int data){ // TODO Auto-generated // constructor stub this.data = data; this.left = null; this.right = null;}} // A stack nodeclass SNode{ SNode head;public Node t;SNode next; // Function to add an element// k to stackpublic void push(Node k){ SNode tmp = new SNode(); // Perform memory check here tmp.t = k; tmp.next = this.head; this.head = tmp;}// Function to pop an element// t from stackpublic Node pop(){ SNode st; st = this.head; head = head.next; return st.t;} // Function to check whether// the stack is empty or notpublic bool isEmpty(){ if (this.head == null ) return true; return false;}}// This code is contributed by Rutvik_56 |
Javascript
<script> // JavaScript program to implement the // above approach // Structure of a BST Node class Node { constructor(data) { // TODO Auto-generated // constructor stub this.data = data; this.left = null; this.right = null; } } // A stack node class SNode { constructor() { this.head = null; this.t = null; this.next = null; } // Function to add an element // k to stack push(k) { var tmp = new SNode(); // Perform memory check here tmp.t = k; tmp.next = this.head; this.head = tmp; } // Function to pop an element // t from stack pop() { var st; st = this.head; this.head = this.head.next; return st.t; } // Function to check whether // the stack is empty or not isEmpty() { if (this.head == null) return true; return false; } } /* A utility function to print Inorder traversal of a Binary Tree */ function inorder(root) { if (root != null) { inorder(root.left); document.write(root.data + " "); inorder(root.right); } } // The function to print data // of two BSTs in sorted order function merge(root1, root2) { // s1 is stack to hold nodes // of first BST var s1 = new SNode(); // Current node of first BST var current1 = root1; // s2 is stack to hold nodes // of second BST var s2 = new SNode(); // Current node of second BST var current2 = root2; // If first BST is empty, then // output is inorder traversal // of second BST if (root1 == null) { inorder(root2); return; } // If second BST is empty, // then output is inorder // traversal of first BST if (root2 == null) { inorder(root1); return; } // Run the loop while there // are nodes not yet printed. // The nodes may be in stack // (explored, but not printed) // or may be not yet explored while ( current1 != null || !s1.isEmpty() || current2 != null || !s2.isEmpty() ) { // Following steps follow // iterative Inorder Traversal if (current1 != null || current2 != null) { // Reach the leftmost node of // both BSTs and push ancestors // of leftmost nodes to stack // s1 and s2 respectively if (current1 != null) { s1.push(current1); current1 = current1.left; } if (current2 != null) { s2.push(current2); current2 = current2.left; } } else { // If we reach a NULL node and // either of the stacks is empty, // then one tree is exhausted, // print the other tree if (s1.isEmpty()) { while (!s2.isEmpty()) { current2 = s2.pop(); current2.left = null; inorder(current2); } return; } if (s2.isEmpty()) { while (!s1.isEmpty()) { current1 = s1.pop(); current1.left = null; inorder(current1); } return; } // Pop an element from both // stacks and compare the // popped elements current1 = s1.pop(); current2 = s2.pop(); // If element of first tree is // smaller, then print it // and push the right subtree. // If the element is larger, // then we push it back to the // corresponding stack. if (current1.data < current2.data) { document.write(current1.data + " "); current1 = current1.right; s2.push(current2); current2 = null; } else { document.write(current2.data + " "); current2 = current2.right; s1.push(current1); current1 = null; } } } document.write(s1.t + "<br>"); document.write(s2.t + "<br>"); } // Driver code var root1 = null, root2 = null; /* Let us create the following tree as first tree 3 / \ 1 5*/ root1 = new Node(3); root1.left = new Node(1); root1.right = new Node(5); /* Let us create the following tree as second tree 4 / \ 2 6*/ root2 = new Node(4); root2.left = new Node(2); root2.right = new Node(6); // Print sorted nodes of // both trees merge(root1, root2); </script> |
1 2 3 4 5 6
Time Complexity: O(m+n)
Auxiliary Space: O(height of the first tree + height of the second tree)
Another simpler version of solving the problem using two stacks is given below :-
In this method I am using inbuilt stack present in the STL library so as to get rid of the implementation of the stack part of code that has done in the previous implementation.
Step 1 :- Consider two stacks s1 and s2 which stores the element of the two trees.
Step 2 :- Store the left view value of a tree1 in s1 and of tree2 in s2.
Step 3 :- Compare the top values present in the stack and push the value accordingly in the result vector.
Case 1 :- if s2 is empty then pop s1 and put the popped node value in the answer vector
else if both s1 and s2 are not empty then compare their top nodes value
if s1.top()->val <= s2.top()->val then in this case push the s1.top()->val in the result vector and push its right child in the stack s1Case 2 :- if s1 is empty then pop s2 and put the popped node value in the answer vector
else if both s1 and s2 are not empty then compare their top nodes value
if s2.top()->val >= s1.top()->val then in this case push the s2.top()->val in the result vector and push its right child in the stack s2Step 4 :- Continue step 3 until both the stacks get empty.
Below is the implementation of the above logic :-
C++
#include <bits/stdc++.h>using namespace std;// Structure of a BST Nodeclass Node {public: int val; Node* left; Node* right;};/* Utility function to create a new Binary Tree Node */Node* newNode(int data){ Node* temp = new Node; temp->val = data; temp->left = nullptr; temp->right = nullptr; return temp;}vector<int> mergeTwoBST(Node* root1, Node* root2){ vector<int> res; stack<Node*> s1, s2; while (root1 || root2 || !s1.empty() || !s2.empty()) { while (root1) { s1.push(root1); root1 = root1->left; } while (root2) { s2.push(root2); root2 = root2->left; } // Step 3 Case 1:- if (s2.empty() || (!s1.empty() && s1.top()->val <= s2.top()->val)) { root1 = s1.top(); s1.pop(); res.push_back(root1->val); root1 = root1->right; } // Step 3 case 2 :- else { root2 = s2.top(); s2.pop(); res.push_back(root2->val); root2 = root2->right; } } return res;}/* Driver program to test above functions */int main(){ Node *root1 = nullptr, *root2 = nullptr; /* Let us create the following tree as first tree 3 / \ 1 5 */ root1 = newNode(3); root1->left = newNode(1); root1->right = newNode(5); /* Let us create the following tree as second tree 4 / \ 2 6 */ root2 = newNode(4); root2->left = newNode(2); root2->right = newNode(6); // Print sorted Nodes of both trees vector<int> ans = mergeTwoBST(root1, root2); for (auto it : ans) cout << it << " "; return 0;}// This code is contributed by Aditya kumar (adityakumar129) |
1 2 3 4 5 6
Time Complexity: O(m+n)
Auxiliary Space: O(height of the first tree + height of the second tree)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




