Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Given two Balanced binary search trees, B1 ha... Start Learning for Free
Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?
  • a)
    O(m+n)
  • b)
    O(mlogn)
  • c)
    O(nlogm)
  • d)
    O(m2 + n2)
Correct answer is option 'A'. Can you explain this answer?
Verified Answer
Given two Balanced binary search trees, B1 having n elements and B2 ha...
O(m+n) as we can first perform inorder on both the trees and store them in two separate arrays. Now we have two sorted sequences and we can merge them in O(m+n) using standard merge algorithm and on the final sorted array we can use the binary search to create the tree using Recursion. Recursively adding middle element at the root and repeating the same process for left and right subarrays.
View all questions of this test
Most Upvoted Answer
Given two Balanced binary search trees, B1 having n elements and B2 ha...
The correct answer is option 'A' - O(mn). Let's understand why.

To merge two balanced binary search trees, we need to follow a specific algorithm that ensures the resulting tree is still balanced.

Here is a step-by-step explanation of the algorithm:

1. Convert B1 and B2 to sorted arrays using an inorder traversal. This step takes O(n) and O(m) time, respectively.

2. Merge the two sorted arrays into a single sorted array using the merge algorithm from Merge Sort. This step takes O(n+m) time.

3. Construct a balanced binary search tree from the merged sorted array. This step takes O(n+m) time.

4. The final balanced binary search tree contains n+m elements.

Now, let's analyze the time complexity of the algorithm:

- Converting B1 and B2 to sorted arrays takes O(n) and O(m) time, respectively.
- Merging the two sorted arrays takes O(n+m) time.
- Constructing a balanced binary search tree from the merged sorted array also takes O(n+m) time.

Therefore, the total time complexity of the algorithm is:

O(n) + O(m) + O(n+m) + O(n+m) = O(n+m) + O(n+m) = 2O(n+m) = O(n+m)

Since n and m are the number of elements in B1 and B2 respectively, the time complexity of the algorithm can be written as O(nm).

Hence, the best known algorithm to merge two balanced binary search trees to form another balanced binary tree containing n+m elements has a time complexity of O(nm), which is equivalent to O(mn). Therefore, the correct answer is option 'A' - O(mn).
Explore Courses for Computer Science Engineering (CSE) exam
Question Description
Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer?.
Solutions for Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer?, a detailed solution for Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer? has been provided alongside types of Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Given two Balanced binary search trees, B1 having n elements and B2 having m elements, what is the time complexity of the best known algorithm to merge these trees to form another balanced binary tree containing m+n elements ?a)O(m+n)b)O(mlogn)c)O(nlogm)d)O(m2+ n2)Correct answer is option 'A'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev