P
puzzlecracker
Do you see any problems with the following algorithm that converts Link
list to Binary Search tree. Can you think of a better solution?
node* LLtoBST(node *head, int num)
{
node *root=head;
int i, mid=(num+1)/2;
if(!head) return NULL;
if(n<=1){
head->prev=NULL;
head->next=NULL;
}
else{
for (int i=0;i<mid;++i,root=root->next)
;
root->prev=LLtoBST(head, mid-1);
root->next=LLtoBST(root->next,mid-1);
}
return root;
}
list to Binary Search tree. Can you think of a better solution?
node* LLtoBST(node *head, int num)
{
node *root=head;
int i, mid=(num+1)/2;
if(!head) return NULL;
if(n<=1){
head->prev=NULL;
head->next=NULL;
}
else{
for (int i=0;i<mid;++i,root=root->next)
;
root->prev=LLtoBST(head, mid-1);
root->next=LLtoBST(root->next,mid-1);
}
return root;
}