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

来源:互联网 发布:淘宝网天天特价入口 编辑:程序博客网 时间:2024/05/14 04:15
#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;}
0 0
原创粉丝点击