|
nced3(pRoot, depth);
}
/*
求二叉树的镜像:
方法1: 前序遍历每个节点,如果遍历到的节点有子节点,就交换它的两个子节点。(先交换左子树和右子树,再对左子树和右子树进行镜像操作)
方法2: 如果二叉树不为空,求左子树和右子树的镜像,然后再交换左子树和右子树
*/
void Mirror(BTree* &pRoot)
{
if(pRoot == NULL)
return;
if(pRoot->m_pLeft ==NULL && pRoot->m_pRight == NULL)
return;
BTree* pTemp = pRoot->m_pLeft;
pRoot->m_pLeft = pRoot->m_pRight;
pRoot->m_pRight = pTemp;
if(pRoot->m_pLeft)
Mirror(pRoot->m_pLeft);
if(pRoot->m_pRight)
Mirror(pRoot->m_pRight);
}
BTree* Mirror2(BTree* pRoot)
{
if(pRoot == NULL)
return NULL;
BTree* pLeft = Mirror2(pRoot->m_pLeft);
BTree* pRight = Mirror2(pRoot->m_pRight);
pRoot->m_pLeft = pRight;
pRoot->m_pRight = pLeft;
return pRoot;
}
void PrintPrev(BTree* pRoot)
{
if(pRoot == NULL)
return;
cout<
m_nValue<<" "; PrintPrev(pRoot->m_pLeft); PrintPrev(pRoot->m_pRight); } int _tmain(int argc, _TCHAR* argv[]) { BTree* m_pRoot = new BTree(9); Insert(3,m_pRoot); Insert(6,m_pRoot); Insert(1,m_pRoot); Insert(2,m_pRoot); Insert(5,m_pRoot); Insert(8,m_pRoot); Insert(7,m_pRoot); Insert(10,m_pRoot); cout<
|