6_43_递归交换二叉树中所有节点的左右子树

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{
    int data;
    struct node*lchild,*rchild;
}tnode,*tree;
tree creat()
{
    int x;
    tree t;
    scanf("%d",&x);
    if(x==0)t=NULL;
    else
    {
        t=(tnode*)malloc(sizeof(tnode));
        t->data=x;
        t->lchild=creat();
        t->rchild=creat();
    }
    return t;
}
void TreeSwap(tree t)
{
    if(t)
    {
        tree p=t->lchild;
        t->lchild=t->rchild;
        t->rchild=p;
        TreeSwap(t->lchild);
        TreeSwap(t->rchild);
    }
}
void preorder(tree t)
{
    if(t)
    {
        printf("%d\n",t->data);
        preorder(t->lchild);
        preorder(t->rchild);
    }
}
int main()
{
    tree t=creat();
    TreeSwap(t);
    preorder(t);
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-04-25 16:16  Thereisnospon  阅读(240)  评论(0编辑  收藏  举报