学习笔记—交换二叉树所有节点中的左右子树

#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;
}

你可能感兴趣的:(二叉树)