573 views
0 votes
0 votes

two (possibly unbalanced) BST's containing n elements each can be merged into a single BST in O(n) time - TRUE

if it was balanced then it would be O(log n) right?

1 Answer

1 votes
1 votes

in both the cases complexity would be O(n)....

procedure:- find inorder (O(n)) traversal of both tree and merge them :-(2O(n) for inorder+O(2n-1)for merging...

therefor O(n) 

Note:-point to be noticed ..in case  of BST Inorder traversal will be in ascending order (sorted array)

Related questions

1.1k
views
1 answers
0 votes
gagan55 asked Jul 13, 2023
1,111 views
How to Visualize this code ?#include<stdio.h> #include<stdlib.h> struct node{ int data; struct node *next; }; void addFirst(struct node **head,int val){ ... addFirst(&head,30); addFirst(&head,40); addFirst(&head,50); printList(head); }
473
views
1 answers
0 votes
kickassakash asked Jul 4, 2023
473 views
I have specific doubt on this question and I’ve tried to explain that in the picture ,If anyone can explain it then it’ll be of great help. according to sachin sir the answer should be false( fyi).
467
views
1 answers
0 votes
Souvik33 asked Apr 2, 2023
467 views
There are Insert and Retrieve_Max operations on a set {}. for n such operations what is the time complexity of the efficient algorithm possible?$n^{2}$nlogn n logn
926
views
1 answers
0 votes
Souvik33 asked Nov 2, 2022
926 views
Which data structure would be most appropriate to implement a collection of values with the following 3 characteristicsSingly link list with head and tail pointerDoubly link list with only head pointerBinary treeArray