二叉树的镜像指的是将所有节点的左右子树互换,
使用递归进行处理非常方便。
void MirrorBiTree(BTNode* pRoot)
{
if (NULL == pRoot)
return;
// 左右子树都不存在
if (NULL == pRoot->pLeft && NULL == pRoot->pRight)
return;
// 交换左右子树
BTNode* pTemp = pRoot->pLeft;
pRoot->pLeft = pRoot->pRight;
pRoot->pRight = pTemp;
if (pRoot->pLeft != NULL)
MirrorBiTree(pRoot->pLeft);
if (pRoot->pRight != NULL)
MirrorBiTree(pRoot->pRight);
}