#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);
}
6_43_递归交换二叉树中所有节点的左右子树
最新推荐文章于 2024-05-11 09:01:17 发布