Merge Two BSTs

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
191 upvotes
Asked in companies
AmazonJosh Technology GroupD.E.Shaw

Problem statement

You are given two binary search trees of integers having ‘N’ and ‘M’ nodes. Return an array that contains elements of both BST in sorted order.


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.


Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line contains the elements of the first BST in the level order form separated by a single space.

The second line contains the elements of the second 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.


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 :
The only line contains an array that contains elements of both BST in sorted order.
Note:
You are not required to print the output, it has already been taken care of. Just implement the function.
Sample Input 1:
2 1 3 -1 -1 -1 -1
4 -1 -1
Sample Output 1:
1 2 3 4 
Explanation For Sample Output 1:
 The given BST are:-

The output will be 1 2 3 4
Sample Input 2:
4 2 7 -1 3 -1 -1 -1 -1  
5 1 7 -1 -1 -1 -1
Sample Output 2:
1 2 3 4 5 7 7 
Constraints:
1 <= 'N', 'M' <= 10^5

Time Limit: 1 sec
Hint

Find inorder of the BST.

Approaches (1)
Binary Search Tree

Approach:

 

  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 ‘answer' in sorted order(increasing order).
  4. Return the â€˜answer’ array.

 

Time Complexity

O(N + M), where ‘N’ and ‘M’ are the number of nodes in the first BST and the second BST respectively.

 

Since both inorder of trees, merging of two sequences and building the tree from the resulting sequence took O(‘N’ + ‘M’) time, so the overall time complexity will be O(‘N’ + ‘M’).

Space Complexity

O(N + M), where ‘N’ and ‘M’ are the number of nodes in the first BST and the second BST respectively.

 

We are storing the inorder traversal of both BST in separate arrays, and the merged inorder traversal in another array of size ‘N’ + ‘M’. So the overall space complexity will be O(‘N’ + ‘M’).

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Merge Two BSTs
Full screen
Console