剑指offer 面试题19:二叉树的镜像

题目:
请完成一个函数,输入一个二叉树,输出它的镜像

struct binaryTreeNode {
    int                        m_nValue;
    binaryTreeNode*  m_pLeft;
    binaryTreeNode*  m_pRight;
};

解法:

void mirrorBinTree(binaryTreeNode* pRoot) {
    if (pRoot == 0 || (pRoot->m_pLeft == 0 && pRoot->m_pRight == 0)) {
        return;
    }
    binaryTreeNode*  tmp  =  pRoot->m_pLeft;
    pRoot->m_pLeft  =  pRoot->m_pRight;
    pRoot->m_pRight  =  tmp;

    if (pRoot->m_pLeft) {
        mirrorBinTree(pRoot->m_pLeft);
    }
    if (pRoot->m_pRight) {
        mirrorBinTree(pRoot->m_pRight);
    }
}

你可能感兴趣的:(剑指offer 面试题19:二叉树的镜像)