代码
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
typedef struct node
{
int data;
node * lchild,*rchild;
int ltag, rtag;
}*Tree;
void build(Tree &T)
{
char ch;
cin>>ch;
if(ch == '#')
T = NULL;
else
{
T=(Tree)malloc(sizeof(node));
T->data=ch;
build(T->lchild);
build(T->rchild);
}
}
void preorder(Tree T)
{
if(T != NULL)
{
printf("%c ",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
void exchange(Tree &T)
{
Tree tp;
if(T!=NULL)
{
tp=T->lchild;
T->lchild=T->rchild;
T->rchild=tp;
exchange(T->lchild);
exchange(T->rchild);
}
}
int main()
{
Tree T;
build(T);
preorder(T);
printf("\n");
exchange(T);
preorder(T);
printf("\n");
return 0;
}
结果