puzzle

how to convrt a binary tree into a doubly linked list?

can anybody come up with the code

i think it requires a bfs traversal of the tree

but is there any better solution

std::cout<<"thanks in advance"<<endl;
node * dlroot =null;
node * q =null;
BtoDL (node *p){
if (p){

BtoDL(p->left);
BtoDL(p->right);
if(dlroot )
{
q->right = p; // putting right and left pointer of traversed node
p->left = q;
q=q->right; //updating node of DL.
}

else { //This part of code will execute only once.
dlroot = p;
dlroot->right=dlroot->left =null;
q=dlroot;
}

}// enf outer if.

} //end of function.


this is the code by the way
Do the nodes in the DLL have to be in some special order with resopect to the original BT?
no need

but the solution i posted is based on postorder traversal

if u cud modify the algorithm to perform the changes while doing an inorder traversal

we ll get the sorted data in doubly linked list

i wonder if we cud perform a bfs or a dfs to do the same

any suggestion

can anybody come up with a better solution
Do any traversal and just make
node * temp = new node ();
and than like it with the previous one in that trversal .. and the code is like this . with bit changes .. i find this way bit easier ,
@above

the main thing is to change the tree into dll.....

not creating a new one
Topic archived. No new replies allowed.