+1 vote
in Data Structures & Algorithms I by (110k points)
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 Answer

+1 vote
by (737k points)
selected by
Best answer
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.

Related questions

Welcome to TalkJarvis QnA, a question-answer community website for the people by the people. On TalkJarvis QnA you can ask your doubts, curiosity, questions and whatever going in your mind either related to studies or others. Experts and people from different fields will answer.