[Approach - 1] Using Array - O(n + m) Time and O(n + m) Space
The idea is to perform inorder traversal of both BSTs to get their elements in sorted order, store them in two arrays (or lists), and then merge these two sorted arrays using a two-pointer approach to produce a single sorted list containing all elements from both BSTs.
C++
//Driver Code Starts#include<iostream>#include<vector>usingnamespacestd;// Node structureclassNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};//Driver Code Ends// Function to perform inorder traversal of a BST// Stores elements in sorted order in the given vectorvoidinorder(Node*root,vector<int>&arr){if(!root)return;inorder(root->left,arr);arr.push_back(root->data);inorder(root->right,arr);}// Function to merge two sorted arrays into one sorted arrayvector<int>mergeArrays(vector<int>&arr1,vector<int>&arr2){vector<int>result;inti=0,j=0;// Traverse both arrays and pick the smaller elementwhile(i<arr1.size()&&j<arr2.size()){if(arr1[i]<=arr2[j]){result.push_back(arr1[i++]);}else{result.push_back(arr2[j++]);}}while(i<arr1.size())result.push_back(arr1[i++]);while(j<arr2.size())result.push_back(arr2[j++]);returnresult;}// Function to merge elements of two BSTs into a single sorted listvector<int>merge(Node*root1,Node*root2){vector<int>arr1,arr2;// Get inorder traversal of both BSTsinorder(root1,arr1);inorder(root2,arr2);returnmergeArrays(arr1,arr2);}//Driver Code Startsintmain(){// Create binary tree 1// 3// / \ // 1 5Node*root1=newNode(3);root1->left=newNode(1);root1->right=newNode(5);// Create binary tree 2// 4// / \ // 2 6Node*root2=newNode(4);root2->left=newNode(2);root2->right=newNode(6);vector<int>res=merge(root1,root2);for(autoval:res)cout<<val<<" ";cout<<endl;return0;}//Driver Code Ends
Java
//Driver Code Startsimportjava.util.ArrayList;// Node structureclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGFG{//Driver Code Ends// Function to perform inorder traversal of a BST// Stores elements in sorted order in the given liststaticvoidinorder(Noderoot,ArrayList<Integer>arr){if(root==null)return;inorder(root.left,arr);arr.add(root.data);inorder(root.right,arr);}// Function to merge two sorted lists into one sorted liststaticArrayList<Integer>mergeArrays(ArrayList<Integer>arr1,ArrayList<Integer>arr2){ArrayList<Integer>result=newArrayList<>();inti=0,j=0;// Traverse both lists and pick the smaller elementwhile(i<arr1.size()&&j<arr2.size()){if(arr1.get(i)<=arr2.get(j)){result.add(arr1.get(i++));}else{result.add(arr2.get(j++));}}while(i<arr1.size())result.add(arr1.get(i++));while(j<arr2.size())result.add(arr2.get(j++));returnresult;}// Function to merge elements of two BSTs into a single sorted liststaticArrayList<Integer>merge(Noderoot1,Noderoot2){ArrayList<Integer>arr1=newArrayList<>();ArrayList<Integer>arr2=newArrayList<>();// Get inorder traversal of both BSTsinorder(root1,arr1);inorder(root2,arr2);returnmergeArrays(arr1,arr2);}//Driver Code Startspublicstaticvoidmain(String[]args){// Create binary tree 1// 3// / \// 1 5Noderoot1=newNode(3);root1.left=newNode(1);root1.right=newNode(5);// Create binary tree 2// 4// / \// 2 6Noderoot2=newNode(4);root2.left=newNode(2);root2.right=newNode(6);ArrayList<Integer>res=merge(root1,root2);for(intval:res)System.out.print(val+" ");System.out.println();}}//Driver Code Ends
Python
#Driver Code Starts# Node structureclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None#Driver Code Ends# Function to perform inorder traversal of a BST# Stores elements in sorted order in the given listdefinorder(root,arr):ifnotroot:returninorder(root.left,arr)arr.append(root.data)inorder(root.right,arr)# Function to merge two sorted lists into one sorted listdefmergeArrays(arr1,arr2):result=[]i=j=0# Traverse both lists and pick the smaller elementwhilei<len(arr1)andj<len(arr2):ifarr1[i]<=arr2[j]:result.append(arr1[i])i+=1else:result.append(arr2[j])j+=1whilei<len(arr1):result.append(arr1[i])i+=1whilej<len(arr2):result.append(arr2[j])j+=1returnresult# Function to merge elements of two BSTs into a single sorted listdefmerge(root1,root2):arr1,arr2=[],[]# Get inorder traversal of both BSTsinorder(root1,arr1)inorder(root2,arr2)returnmergeArrays(arr1,arr2)#Driver Code Startsif__name__=="__main__":# Create binary tree 1# 3# / \# 1 5root1=Node(3)root1.left=Node(1)root1.right=Node(5)# Create binary tree 2# 4# / \# 2 6root2=Node(4)root2.left=Node(2)root2.right=Node(6)res=merge(root1,root2)forvalinres:print(val,end=" ")print()#Driver Code Ends
C#
//Driver Code StartsusingSystem;usingSystem.Collections.Generic;// Node structureclassNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGFG{//Driver Code Ends// Function to perform inorder traversal of a BST// Stores elements in sorted order in the given liststaticvoidinorder(Noderoot,List<int>arr){if(root==null)return;inorder(root.left,arr);arr.Add(root.data);inorder(root.right,arr);}// Function to merge two sorted lists into one sorted liststaticList<int>mergeArrays(List<int>arr1,List<int>arr2){List<int>result=newList<int>();inti=0,j=0;// Traverse both lists and pick the smaller elementwhile(i<arr1.Count&&j<arr2.Count){if(arr1[i]<=arr2[j]){result.Add(arr1[i++]);}else{result.Add(arr2[j++]);}}while(i<arr1.Count)result.Add(arr1[i++]);while(j<arr2.Count)result.Add(arr2[j++]);returnresult;}// Function to merge elements of two BSTs into a single sorted liststaticList<int>merge(Noderoot1,Noderoot2){List<int>arr1=newList<int>();List<int>arr2=newList<int>();// Get inorder traversal of both BSTsinorder(root1,arr1);inorder(root2,arr2);returnmergeArrays(arr1,arr2);}//Driver Code StartsstaticvoidMain(){// Create binary tree 1// 3// / \// 1 5Noderoot1=newNode(3);root1.left=newNode(1);root1.right=newNode(5);// Create binary tree 2// 4// / \// 2 6Noderoot2=newNode(4);root2.left=newNode(2);root2.right=newNode(6);List<int>res=merge(root1,root2);foreach(intvalinres)Console.Write(val+" ");Console.WriteLine();}}//Driver Code Ends
JavaScript
//Driver Code Starts// Node structureclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}//Driver Code Ends// Function to perform inorder traversal of a BST// Stores elements in sorted order in the given arrayfunctioninorder(root,arr){if(!root)return;inorder(root.left,arr);arr.push(root.data);inorder(root.right,arr);}// Function to merge two sorted arrays into one sorted arrayfunctionmergeArrays(arr1,arr2){letresult=[];leti=0,j=0;// Traverse both arrays and pick the smaller elementwhile(i<arr1.length&&j<arr2.length){if(arr1[i]<=arr2[j]){result.push(arr1[i++]);}else{result.push(arr2[j++]);}}while(i<arr1.length)result.push(arr1[i++]);while(j<arr2.length)result.push(arr2[j++]);returnresult;}// Function to merge elements of two BSTs into a single sorted arrayfunctionmerge(root1,root2){letarr1=[],arr2=[];// Get inorder traversal of both BSTsinorder(root1,arr1);inorder(root2,arr2);returnmergeArrays(arr1,arr2);}//Driver Code Starts// Driver Code// Create binary tree 1// 3// / \// 1 5letroot1=newNode(3);root1.left=newNode(1);root1.right=newNode(5);// Create binary tree 2// 4// / \// 2 6letroot2=newNode(4);root2.left=newNode(2);root2.right=newNode(6);letres=merge(root1,root2);console.log(res.join(" "));//Driver Code Ends
Output
1 2 3 4 5 6
[Approach - 2] Using Stack - O(n + m) Time and O(n + m) Space
The idea is to simulate the inorder traversal of both trees simultaneously using two stacks, comparing the current minimum elements from both trees at each step and selecting the smaller one to add to the result.
Step by step approach:
Push all left nodes of both trees onto their respective stacks until reaching leftmost nodes.
Compare the top elements of both stacks to determine which has smaller value.
Pop the smaller element, add it to result, and move to its right subtree.
Repeat the process until both stacks are empty and no more nodes to process.
Why using Stack?
We can’t use recursion here because it’s not possible to traverse both trees simultaneously — recursion can only process one call stack at a time. In other words, we can’t pause recursion in one tree, switch to the other, and then resume. To handle both traversals together and compare nodes dynamically (for example, to stop when one node’s value exceeds the other’s), we use an iterative approach with stacks.
C++
//Driver Code Starts#include<iostream>#include<vector>#include<stack>usingnamespacestd;// Node structureclassNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};//Driver Code Endsvector<int>merge(Node*root1,Node*root2){vector<int>res;stack<Node*>s1,s2;while(root1||root2||!s1.empty()||!s2.empty()){// move to the leftmost nodes(min values)while(root1){s1.push(root1);root1=root1->left;}while(root2){s2.push(root2);root2=root2->left;}// compare the top element and remove // it and move to its right childif(s2.empty()||(!s1.empty()&&s1.top()->data<=s2.top()->data)){root1=s1.top();s1.pop();res.push_back(root1->data);root1=root1->right;}else{root2=s2.top();s2.pop();res.push_back(root2->data);root2=root2->right;}}returnres;}//Driver Code Startsintmain(){// Create binary tree 1// 3// / \ // 1 5Node*root1=newNode(3);root1->left=newNode(1);root1->right=newNode(5);// Create binary tree 2// 4// / \ // 2 6Node*root2=newNode(4);root2->left=newNode(2);root2->right=newNode(6);vector<int>res=merge(root1,root2);for(autoval:res)cout<<val<<" ";cout<<endl;return0;}//Driver Code Ends
Java
//Driver Code Startsimportjava.util.ArrayList;importjava.util.Stack;// Node structureclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGFG{//Driver Code EndsstaticArrayList<Integer>merge(Noderoot1,Noderoot2){ArrayList<Integer>res=newArrayList<>();Stack<Node>s1=newStack<>();Stack<Node>s2=newStack<>();while(root1!=null||root2!=null||!s1.empty()||!s2.empty()){// move to the leftmost nodes(min values)while(root1!=null){s1.push(root1);root1=root1.left;}while(root2!=null){s2.push(root2);root2=root2.left;}// compare the top element and remove // it and move to its right childif(s2.empty()||(!s1.empty()&&s1.peek().data<=s2.peek().data)){root1=s1.pop();res.add(root1.data);root1=root1.right;}else{root2=s2.pop();res.add(root2.data);root2=root2.right;}}returnres;}//Driver Code Startspublicstaticvoidmain(String[]args){// Create binary tree 1// 3// / \// 1 5Noderoot1=newNode(3);root1.left=newNode(1);root1.right=newNode(5);// Create binary tree 2// 4// / \// 2 6Noderoot2=newNode(4);root2.left=newNode(2);root2.right=newNode(6);ArrayList<Integer>res=merge(root1,root2);for(intval:res)System.out.print(val+" ");System.out.println();}}//Driver Code Ends
Python
#Driver Code Starts# Node structureclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None#Driver Code Endsdefmerge(root1,root2):res=[]s1=[]s2=[]whileroot1orroot2ors1ors2:whileroot1:# move to the leftmost nodes(min values)s1.append(root1)root1=root1.leftwhileroot2:s2.append(root2)root2=root2.left# compare the top element and remove # it and move to its right childifnots2or(s1ands1[-1].data<=s2[-1].data):root1=s1.pop()res.append(root1.data)root1=root1.rightelse:root2=s2.pop()res.append(root2.data)root2=root2.rightreturnres#Driver Code Startsif__name__=="__main__":# Create binary tree 1# 3# / \# 1 5root1=Node(3)root1.left=Node(1)root1.right=Node(5)# Create binary tree 2# 4# / \# 2 6root2=Node(4)root2.left=Node(2)root2.right=Node(6)res=merge(root1,root2)forvalinres:print(val,end=" ")print()#Driver Code Ends
C#
//Driver Code StartsusingSystem;usingSystem.Collections.Generic;// Node structureclassNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGFG{//Driver Code EndsstaticList<int>merge(Noderoot1,Noderoot2){List<int>res=newList<int>();Stack<Node>s1=newStack<Node>();Stack<Node>s2=newStack<Node>();while(root1!=null||root2!=null||s1.Count>0||s2.Count>0){// move to the leftmost nodes(min values)while(root1!=null){s1.Push(root1);root1=root1.left;}while(root2!=null){s2.Push(root2);root2=root2.left;}// compare the top element and remove // it and move to its right childif(s2.Count==0||(s1.Count>0&&s1.Peek().data<=s2.Peek().data)){root1=s1.Pop();res.Add(root1.data);root1=root1.right;}else{root2=s2.Pop();res.Add(root2.data);root2=root2.right;}}returnres;}//Driver Code StartsstaticvoidMain(){// Create binary tree 1// 3// / \// 1 5Noderoot1=newNode(3);root1.left=newNode(1);root1.right=newNode(5);// Create binary tree 2// 4// / \// 2 6Noderoot2=newNode(4);root2.left=newNode(2);root2.right=newNode(6);List<int>res=merge(root1,root2);foreach(intvalinres)Console.Write(val+" ");Console.WriteLine();}}//Driver Code Ends
JavaScript
//Driver Code Starts// Node structureclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}//Driver Code Endsfunctionmerge(root1,root2){letres=[];lets1=[];lets2=[];while(root1||root2||s1.length>0||s2.length>0){while(root1){// move to the leftmost nodes(min values)s1.push(root1);root1=root1.left;}while(root2){s2.push(root2);root2=root2.left;}// compare the top element and remove // it and move to its right childif(s2.length===0||(s1.length>0&&s1[s1.length-1].data<=s2[s2.length-1].data)){root1=s1.pop();res.push(root1.data);root1=root1.right;}else{root2=s2.pop();res.push(root2.data);root2=root2.right;}}returnres;}//Driver Code Starts// Create binary tree 1// 3// / \// 1 5letroot1=newNode(3);root1.left=newNode(1);root1.right=newNode(5);// Create binary tree 2// 4// / \// 2 6letroot2=newNode(4);root2.left=newNode(2);root2.right=newNode(6);letres=merge(root1,root2);console.log(...res);//Driver Code Ends