编写递归算法,交换二叉树的左右子树,输出按先序遍历得到的新二叉树结果。 #include <stdio.h> #include <stdlib.h> typedef struct BitNode{ char data; struct BitNode *lchild,*rchild; }BitTree; BitTree *CreateBitTree (BitTree *&T)//先序建立二叉树,注意参数传递!! { char ch; scanf ("%c",&ch); if (ch == ' ') T = NULL; else{ if (!(T = (BitTree * )malloc(sizeof(BitTree)))) printf("Error/n"); T ->data = ch; CreateBitTree (T ->lchild); CreateBitTree (T ->rchild); } return T; } void preorder(BitTree *T) { if(T!=NULL) { printf("%c",T->data); if(T->lchild!=NULL||T->rchild!=NULL) { printf("("); preorder(T->lchild); if(T->rchild!=NULL) printf(","); preorder(T->rchild); printf(")"); } } } BitTree *exchage(BitTree *&T) { BitTree *p; if (T!=NULL) { p = T ->lchild; T ->lchild = T ->rchild; T ->rchild = p; exchage(T ->lchild); exchage(T ->rchild); } return T; } void main() { BitTree *root=NULL; printf("input the tree/n"); root = CreateBitTree(root); preorder(root); root = exchage (root); printf("/n"); preorder(root); }