題目:
請完成一個函數(shù)两嘴,輸入一個二叉樹恤浪,輸出它的鏡像
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);
}
}