685 views
7 votes
7 votes
Two balanced binary search trees are given with $m$ and $n$ elements respectively. They can be merged into another balanced binary search tree in $\Theta((m+n)^a {(\log (m+n))}^b)$ time. $2.a + 3b =$ _____

1 Answer

Best answer
19 votes
19 votes

By doing an inorder traversal of the given BSTs, we can get the inorder of the merged BST in $O(m)+O(n) = O(m+n).$ Once we have the inorder of the merged BST, we can construct this tree as follows.

  1. Make the middle element the root
  2. Make the middle of the left subarray as the left child
  3. Make the middle of the right subarray as the right child
  4. Recursively do steps 1-3 until all elements of array are processed

Time complexity of above $=O(m+n)$ as no element is processed more than once.

Thus we get $a = 1, b = 0.$

Correct Answer $=2\times 1 = 2.$

selected by
Answer:

Related questions

3 votes
3 votes
1 answer
1
gatecse asked Aug 9, 2020
408 views
The maximum number of leaf nodes in a tree with degree $5$ and height (starting from $1)\;4$ is __________
1 votes
1 votes
1 answer
3
gatecse asked Aug 9, 2020
185 views
What is the value of the postfix expression $8 \ 7 \ 2 \ 4 \ + \ – \ *?$: