What time in terms of Big O will it take to merge two BST's in One?
Each having no of nodes n and height O(log n) with no common element.
Resultant should be also a BST
binary treecdata structures
What time in terms of Big O will it take to merge two BST's in One?
Each having no of nodes n and height O(log n) with no common element.
Resultant should be also a BST
Best Answer
The StackOverflow post @manlio pointed out is an exact duplicate. Basically, yes, the algorithm can improved to O(n+m); the approach is to flatten the trees to sorted lists, merge them, and recreate a BST. This page also has some example code that may be of interest as well.