void flatten(BiTree root) {
// base case
if (root == NULL) return;
// 先递归拉平左右子树
flatten(root->lchild);
flatten(root->rchild);
/****后序遍历位置****/
// 1、左右子树已经被拉平成一条链表
BiTree Left = root->lchild;
BiTree Right = root->rchild;
// 2、将左子树作为右子树
root->lchild = NULL;
root->rchild = Left;
// 3、将原先的右子树接到当前右子树的末端
BiTree p = root;
while (p->rchild != NULL) {
p = p->rchild;
}
p->rchild = Right;
}
二叉树展开为链表
最新推荐文章于 2024-07-24 17:54:05 发布