#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct Node
{
char data;
struct Node *Lchild;
struct Node *Rchild;
}BiTNode,*BiTree;
BiTree Creat()
{
char ch;
BiTNode *S;
ch = getchar();
if(ch=='#')
{
return NULL;
}
S = (BiTNode *)malloc(sizeof(BiTNode));
S->data = ch;
S->Lchild = Creat();
S->Rchild = Creat();
return S;
}
void PrintZX(BiTree S)
{
if(S)
{
PrintZX(S->Lchild);
printf("%c ",S->data );
PrintZX(S->Rchild);
}
}
void Exchange_Tree( BiTree S)
{
if(S)
{
BiTree term;//用相同的数据类型来记录
term = S->Lchild;
S->Lchild = S->Rchild;
S->Rchild = term;
Exchange_Tree(S->Lchild);
Exchange_Tree(S->Rchild);
}
}
int main()
{
BiTree T;
T=Creat();
getch();
PrintZX(T);
printf("\n");
Exchange_Tree(T);
PrintZX(T);
getch();
return 0;
}
学习笔记—交换二叉树所有节点中的左右子树
最新推荐文章于 2024-05-06 21:11:15 发布