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

Hương Trang

New member
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)
 
Answer: a
Explanation: 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.
 

Tìm kiếm

Back
Top