5.txt

来自「清华大学出版社严蔚敏编著数据结构习题答案」· 文本 代码 · 共 27 行

TXT
27
字号
6.43③  编写递归算法,将二叉树中所有结点的
左、右子树相互交换。

要求实现下列函数:
void Exchange(BiTree &bt);
/* Exchange the left and right leaves of */
/* bitree whose root node is bt          */

二叉链表类型定义:
typedef struct BiTNode {
    TElemType data;
    BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;

void Exchange(BiTree &bt)
/* Exchange the left and right leaves of */
/* bitree whose root node is bt          */
{
 BiTree p,q;
 p=bt;
 q=p->lchild;
 p->lchild=p->rchild;
 p->rchild=q;
 if(p->lchild) Exchange(bt->lchild);
 if(p->rchild) Exchange(bt->rchild);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?