递归解法:
(1)如果二叉树为空,返回空
(2)如果二叉树不为空,求左子树和右子树的镜像,然后交换左子树和右子树
BinaryTreeNode *Mirror(BinaryTreeNode *pRoot)
{
if (pRoot == NULL)
return NULL;
BinaryTreeNode *pLeft = Mirror(pRoot->lchild); // 求左子树的镜像
BinaryTreeNode *pRight = Mirror(pRoot->rchild); // 求右子树的镜像
pRoot->lchild = pRight;
pRoot->rchild = pLeft;
return pRoot;
}
本文介绍了一种通过递归方式实现二叉树镜像的方法。首先判断二叉树是否为空,若为空则直接返回空;若不为空,则分别求取左子树和右子树的镜像,并进行左右子树的交换。
393

被折叠的 条评论
为什么被折叠?



