Merge Two BSTs

Posted: 3 Dec, 2020
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

You are given two balanced binary search trees of integers having ‘N’ and ‘M’ nodes. You have to merge the two BSTs into a balanced binary search tree and return the root node to that balanced BST.

A binary search tree (BST) is a binary tree data structure with the following properties.

• The left subtree of a node contains only nodes with data less than the node’s data.

• The right subtree of a node contains only nodes with data greater than the node’s data.

• Both the left and right subtrees must also be binary search trees.


Input format :
The first line contains an Integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow. 

The first line of each test case contains the elements of the first BST in the level order form separated by a single space.

If any node does not have a left or right child, take -1 in its place. Refer to the example below.

The second line of each test case contains the elements of the second BST in the level order form separated by a single space.


Example:
Elements are in the level order form. The input consists of values of nodes separated by a single space in a single line. In case a node is null, we take -1 in its place.

The input for the tree depicted in the below image would be :

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation :

Level 1 :
The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null (-1).
Note:
The above format was just to provide clarity on how the input is formed for a given tree. 

The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1     
Output Format :
For each test case print ‘N’ + ‘M’ space-separated integers representing the inorder traversal of the balanced binary search tree which is formed by merging both the given trees.

Output for each test case will be printed in a separate line.
Note:
You are not required to print the output, it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 50
1 <= N <= 1000
1 <= M <= 1000

Time Limit: 1 sec
Approach 1

In this approach we will construct the merged BST, by merging the inorder of traversal of both the BST. By merging the inorder traversal of both BST we will get the inorder traversal of required BST. Then we will form the BST with that inorder traversal. 

 

The steps are as follows: 

 

  1. Do the inorder traversal of first BST and store the inorder traversal in an array say ‘TEMP1’
  2. Do the inorder traversal of second BST and store the inorder traversal in an array say ‘TEMP2’
  3. Now ‘TEMP1’ and ‘TEMP2’ are two arrays sorted in ascending order. Merge ‘TEMP1’ and ‘TEMP2’ in one array ‘FINAL' in sorted order(increasing order).
  4. From the resultant array  ‘FINAL’ , construct the binary search tree using divide and conquer method as follows:
    1. Declare a variable ‘X’ of type integer, and initialize it with ‘N’ + ‘M’ which is also the size of ‘FINAL’ array.
    2. Select middle element lets say ‘MID’ of array ‘FINAL’ as root of the binary tree
    3. Recursively get left child by working on range [1, ‘MID’ - 1].
    4. Recursively get right child by working on range [‘ MID’ + 1, ‘X’]
    5. If in the recursion if its not valid range return ‘NULL’
    6. Assign the left child and right to the root node.

     5. Return the root of the binary tree.

Try Problem