Merge Two BSTs

Given two binary search tree A and B of size N and M.
Return an array of integers containing elements of both BST in non-decreasing order.

Note:

  1. Expected time complexity O(M+N)

  2. Expected Auxillary Space (H(A) + H(B)) where H(A) is height of tree A and H(B) is height of tree B.

Constraints

1 <= N, M <= 100000

For Example

Input 1:
            5            7        
           / \          / \
          3   8        2   9
     
Output 1:
    [2, 3, 5, 7, 8, 9]

Input 1:
            1            5        
             \          / \
              4        2  10
     
Output 1:
    [1, 2, 4, 5, 10]    
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Merge Two BSTs on Interview Code Editor
Sign Up
to access hints and editorial solutions for Merge Two BSTs

Discussion


Loading...
Click here to start solving coding interview questions