数据结构之复制二叉树

#include <iostream>


using namespace std;


struct BitNode
{
char data;
BitNode *lchild;
BitNode *rchild;
};


void create(BitNode *&T)
{
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
T=new BitNode;
T->data=ch;
create(T->lchild);
create(T->rchild);
}
}


void proOrder(BitNode *T)
{
if(T)
{
cout<<T->data<<" ";
proOrder(T->lchild);
proOrder(T->rchild);
}

}


void copyTree(BitNode *T,BitNode *&newT)
{
if(T==NULL)
{
newT=NULL;
}
else
{
newT=new BitNode;
newT->data=T->data;
copyTree(T->lchild,newT->lchild);
copyTree(T->rchild,newT->rchild);
}
}


int main()
{
BitNode *T1,*T2;
create(T1);
copyTree(T1,T2);

proOrder(T2);


return 0;
}

猜你喜欢

转载自blog.csdn.net/wrc_nb/article/details/80343178