Given two height-balanced binary search trees, in-place merge them into a single balanced binary search tree. For each node of a height-balanced tree, the difference between its left and right subtree height is at most 1.

For example,

Input: Below BSTs
 
     20
    /  \
   10  30
       / \
      25 100
 
 
     50
    /  \
   5   70
 
Output:
 
          30
        /    \
      20     70
     / \     / \
    10 25   50 100
   /
  5
 
OR
 
       25
     /    \
    10     50
   /  \   /  \
  5   20 30  70
               \
               100
 
OR
 
Any other possible representation.

Practice this problem

A simple solution is to perform postorder traversal on the smaller tree and insert each encountered “node” into the larger BST. Insertion into a height-balanced BST of size n takes O(log(n)) time. Therefore, the overall time complexity of this approach will be O(m.log(n)) for m keys in the smaller BST and n keys in the larger BST. We can improve the time complexity by using any of the following methods:

1. Using Extra space

The idea is to perform the inorder traversal on both BSTs and store the traversals in two different arrays. These arrays would be in sorted order since the BST inorder traversal traverses the nodes in sorted order. Then merge the sorted arrays into a single array using logic similar to the merge routine of the merge sort algorithm. Finally, construct a height-balanced BST from the sorted keys using logic discussed in the following post.

Construct a balanced BST from the given keys

 
Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

25 10 5 20 50 30 70 100

Java


Download  Run Code

Output:

25 10 5 20 50 30 70 100

Python


Download  Run Code

Output:

25 10 5 20 50 30 70 100

The time complexity of the above solution is O(m + n), where m is the total number of nodes in the first BST and n is the total number of nodes in the second BST. The additional space used by the program is O(m + n).

2. In-Place Solution

The above solution is simple and works in linear time but uses extra space for storing the inorder traversals. This problem can be solved in-place without using the extra space. The idea is to convert both binary search trees into a sorted doubly linked list and then merge both lists into a single, doubly linked list. Finally, construct a height-balanced BST from the sorted doubly linked list. Refer to the above posts for implementation details.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

30 20 10 5 25 70 50 100

Java


Download  Run Code

Output:

30 20 10 5 25 70 50 100

Python


Download  Run Code

Output:

30 20 10 5 25 70 50 100

The time complexity of the above solution is O(m + n), where m is the total number of nodes in the first BST and n is the total number of nodes in the second BST. The conversion is done in-place. However, since recursion is involved, the space used by the call stack is O(x + y), where x is the height of the first tree, and y is the height of the second tree.