You are given two balanced binary search trees e.g., AVL or Red Black Tree. Write a function that merges the two given balanced BSTs into a balanced binary search tree. Let there be m elements in first tree and n elements in the other tree. Your merge function should take O(m+n) time.
In the following solutions, it is assumed that sizes of trees are also given as input. If the size is not given, then we can get the size by traversing the tree (See this).
Method 1 (Insert elements of first tree to second)
Take all elements of first BST one by one, and insert them into the second BST. Inserting an element to a self balancing BST takes Logn time (See this) where n is size of the BST. So time complexity of this method is Log(n) + Log(n+1) … Log(m+n-1). The value of this expression will be between mLogn and mLog(m+n-1). As an optimization, we can pick the smaller tree as first tree.
Method 2 (Merge Inorder Traversals)
1) Do inorder traversal of first tree and store the traversal in one temp array arr1. This step takes O(m) time.
2) Do inorder traversal of second tree and store the traversal in another temp array arr2. This step takes O(n) time.
3) The arrays created in step 1 and 2 are sorted arrays. Merge the two sorted arrays into one array of size m + n. This step takes O(m+n) time.
4) Construct a balanced tree from the merged array using the technique discussed in this post. This step takes O(m+n) time.
Time complexity of this method is O(m+n) which is better than method 1. This method takes O(m+n) time even if the input BSTs are not balanced.
Following is implementation of this method.
Following is Inorder traversal of the merged tree 20 40 50 70 80 100 120 300
Method 3 (In-Place Merge using DLL)
We can use a Doubly Linked List to merge trees in place. Following are the steps.
1) Convert the given two Binary Search Trees into doubly linked list in place (Refer this post for this step).
2) Merge the two sorted Linked Lists (Refer this post for this step).
3) Build a Balanced Binary Search Tree from the merged list created in step 2. (Refer this post for this step)
Time complexity of this method is also O(m+n) and this method does conversion in place.
Thanks to Dheeraj and Ronzii for suggesting this method.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to determine if a binary tree is height-balanced?
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Sorted merge of two sorted doubly circular linked lists
- In-place conversion of Sorted DLL to Balanced BST
- How to handle duplicates in Binary Search Tree?
- Treap (A Randomized Binary Search Tree)
- Merge Sort for Doubly Linked List
- Merge K sorted Doubly Linked List in Sorted Order
- proto van Emde Boas Trees | Set 1 (Background and Introduction)
- Splay Tree | Set 1 (Search)
- Search an Element in Doubly Circular Linked List
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Why is Binary Heap Preferred over BST for Priority Queue?
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Convert a Binary Tree to a Circular Doubly Link List