Create a sorted singly linked list out of a sorted BST.
tree* foo(tree* root)
{
if(!root) return NULL;
tree* A=foo(root->left);
tree* B=foo(root->right);
return append(A,root,B);
}
tree* append(tree* A, tree* root, tree* B)
{
tree* A1=A;
if(A)
{
while(A1->next)
A1=A1->next;
A1->next=root;
root=A;
}
tree* B1=root;
if(B)
{
while(B1->next)
B1=B1->next;
B1->next=B;
}
return root;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment