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).
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.
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.