Two balanced binary trees are given with m and n elements respectively. They can be merged into a balanced binary search tree in ____ time.

Category: QuestionsTwo balanced binary trees are given with m and n elements respectively. They can be merged into a balanced binary search tree in ____ time.
Editor">Editor Staff asked 4 weeks ago

Two balanced binary trees are given with m and n elements respectively. They can be merged into a balanced binary search tree in ____ time.
 
(a) O(m+n)
 
(b) O(mn)
 
(c) O(m)
 
(d) O(mlog n)
 
My question is taken from Binary Trees topic in chapter Binary Trees of Data Structures & Algorithms I
 
The question was posed to me in a job interview.

1 Answers
Editor">Editor Staff answered 4 weeks ago

Correct option is (a) O(m+n)
 
To explain: First we store the in-order traversals of both the trees in two separate arrays and then we can merge these sorted sequences in O(m+n) time. And then we construct the balanced tree from this final sorted array.