二叉树镜像(递归和非递归):
// 求二叉树的镜像:非递归
void GetBinaryMirror_Nor()
{
if(_pRoot == NULL)
return;
stack<Node*> s;
s.push(_pRoot);
while(!s.empty())
{
Node* pCur = NULL;
pCur = s.top();
s.pop();
if(pCur->_pLeft || pCur->_pRight)
swap(pCur->_pLeft,pCur->_pRight);
if(pCur->_pRight)
s.push(pCur->_pRight);
if(pCur->_pLeft)
s.push(pCur->_pLeft);
}
}
// 求二叉树的镜像:递归版本
void GetBinaryMirror()
{
_GetBinaryMirror(_pRoot);
}
void _GetBinaryMirror(Node* &pRoot)
{
if(pRoot == NULL)
return;
if(pRoot->_pLeft == NULL && pRoot->_pRight == NULL)
return;
swap(pRoot->_pLeft,pRoot->_pRight);
_GetBinaryMirror(pRoot->_pLeft);
_GetBinaryMirror(pRoot->_pRight);
}