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 first tree and n is the number of nodes in second tree. 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 already discussed methods of the previous post which was for Balanced BSTs. The method 1 can be applied here also, but the time complexity will be O(n^2) in worst case. The 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 the 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 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.
- Merge two BSTs with constant extra space
- Merge Sort with O(1) extra space merge and O(n lg n) time
- Merge two sorted arrays with O(1) extra space
- K'th smallest element in BST using O(1) Extra Space
- K'th Largest element in BST using constant extra space
- Find duplicates in O(n) time and O(1) extra space | Set 1
- Connect nodes at same level using constant extra space
- Efficiently merging two sorted arrays with O(1) extra space
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Find the maximum repeating number in O(n) time and O(1) extra space
- Length of longest palindrome list in a linked list using O(1) extra space
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
- Check if two BSTs contain same set of elements
- Construct all possible BSTs for keys 1 to N
Improved By : rathbhupendra