交换左右子树
#include<iostream>
using namespace std;
typedef struct TNode
{
int data;
struct TNode *lchild;
struct TNode *rchild;
}TNode;
void order(TNode *t);
void create(TNode *&t)
{
int data;
cin >> data;
if(data==0)
{
t=NULL;
}
else
{
t=(TNode*)malloc(sizeof(TNode));
t->data=data;
create(t->lchild);
create(t->rchild);
}
}
void order(TNode *t)
{
if(t!=NULL)
{
cout<<t->data<<endl;
order(t->lchild);
order(t->rchild);
}
}
void exchange(TNode *t)
{
if(t!=NULL)
{
TNode *temp=t->lchild;
t->lchild=t->rchild;
t->rchild=temp;
exchange(t->lchild);
exchange(t->rchild);
}
}
int main()
{
TNode *t;
create(t);
cout << "先序序列" << endl;
order(t);
exchange(t);
cout << "交换左右子树" << endl;
order(t);
return 0;