题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像。
解题方法:遇到复杂问题,可以通过画图、举例等方法,来让自己加深理解。思路往往就在你一步步的分析之中。
思路:遍历这颗树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点。交换完所有非叶子结点的左右孩子结点后,就得到了树的镜像。
写递归时的想法;该设置的退出条件设置好,该进行的交换操作写好,当孩子结点不为空的时候,就去递归孩子结点。
#include "TreeNode.h" /*头文件中包含二叉树的结构定义,创建、遍历函数等*/
//using namespace std;
/*输出镜像*/
TreeNode * mirror_Recursively(TreeNode *pRoot)
{
if(NULL == pRoot)
return pRoot;
if(NULL == pRoot->lchild && NULL == pRoot->rchild)
return pRoot;
TreeNode *temp;
temp = pRoot->lchild;
pRoot->lchild = pRoot->rchild;
pRoot->rchild = temp;
if(pRoot->lchild) /**如果左孩子不为空,就继续递归它的左孩子*/
mirror_Recursively(pRoot->lchild);
if(pRoot->rchild) /**如果右孩子不为空,就继续递归它的右孩子*/
mirror_Recursively(pRoot->rchild);
return pRoot;
}
/** 树中结点含有分叉,树B是树A的子结构 */
/** 8 */
/** / \ */
/** 8 7 */
/** / \ */
/** 9 2 */
/** / \ */
/** 4 7 */
void Test1()
{
TreeNode* pNodeA1 = create_Treenode(8);
TreeNode* pNodeA2 = create_Treenode(8);
TreeNode* pNodeA3 = create_Treenode(7);
TreeNode* pNodeA4 = create_Treenode(9);
TreeNode* pNodeA5 = create_Treenode(2);
TreeNode* pNodeA6 = create_Treenode(4);
TreeNode* pNodeA7 = create_Treenode(7);
connect_Treenode(pNodeA1, pNodeA2, pNodeA3);
connect_Treenode(pNodeA2, pNodeA4, pNodeA5);
connect_Treenode(pNodeA5, pNodeA6, pNodeA7);
PreOrder(pNodeA1); /*先序遍历,通过这种遍历方式,来检查镜像是否出错*/
mirror_Recursively(pNodeA1);
cout << endl;
PreOrder(pNodeA1);
//Test("Test1", pNodeA1, pNodeB1, true);
//DestroyTree(pNodeA1);
//DestroyTree(pNodeB1);
}
/** 树中结点只有左子结点,树B是树A的子结构 */
/** 8 */
/** / */
/** 8 */
/** / */
/** 9 */
/** / */
/** 2 */
/** / */
/** 5 */
void Test3()
{
TreeNode* pNodeA1 = create_Treenode(8);
TreeNode* pNodeA2 = create_Treenode(8);
TreeNode* pNodeA3 = create_Treenode(9);
TreeNode* pNodeA4 = create_Treenode(2);
TreeNode* pNodeA5 = create_Treenode(5);
connect_Treenode(pNodeA1, pNodeA2, NULL);
connect_Treenode(pNodeA2, pNodeA3, NULL);
connect_Treenode(pNodeA3, pNodeA4, NULL);
connect_Treenode(pNodeA4, pNodeA5, NULL);
InOrder(pNodeA1); /*中序遍历*/
mirror_Recursively(pNodeA1);
cout << endl;
InOrder(pNodeA1);
//DestroyTree(pNodeA1);
//DestroyTree(pNodeB1);
}
/** 树中结点只有右子结点,树B是树A的子结构 */
/** 8 */
/** \ */
/** 8 */
/** \ */
/** 9 */
/** \ */
/** 2 */
/** \ */
/** 5 */
void Test5()
{
TreeNode* pNodeA1 = create_Treenode(8);
TreeNode* pNodeA2 = create_Treenode(8);
TreeNode* pNodeA3 = create_Treenode(9);
TreeNode* pNodeA4 = create_Treenode(2);
TreeNode* pNodeA5 = create_Treenode(5);
connect_Treenode(pNodeA1, NULL, pNodeA2);
connect_Treenode(pNodeA2, NULL, pNodeA3);
connect_Treenode(pNodeA3, NULL, pNodeA4);
connect_Treenode(pNodeA4, NULL, pNodeA5);
InOrder(pNodeA1);
mirror_Recursively(pNodeA1);
cout << endl;
InOrder(pNodeA1);
//DestroyTree(pNodeA1);
//DestroyTree(pNodeB1);
}
/** 树A为空树 */
void Test7()
{
TreeNode* pNodeB1 = create_Treenode(8);
TreeNode* pNodeB2 = create_Treenode(9);
TreeNode* pNodeB3 = create_Treenode(3);
TreeNode* pNodeB4 = create_Treenode(2);
connect_Treenode(pNodeB1, NULL, pNodeB2);
connect_Treenode(pNodeB2, pNodeB3, pNodeB4);
InOrder(pNodeB1);
pNodeB1 = mirror_Recursively(NULL);
cout << endl;
InOrder(pNodeB1);
//DestroyTree(pNodeB1);
}
/**一个结点*/
void Test8()
{
TreeNode* pNodeB1 = create_Treenode(8);
InOrder(pNodeB1);
mirror_Recursively(pNodeB1);
cout << endl;
InOrder(pNodeB1);
//DestroyTree(pNodeB1);
}
int main()
{
Test1();
cout << endl;
Test3();
cout << endl;
Test5();
cout << endl;
Test7();
cout << endl;
Test8();
return 0;
}
结果如下:
/*点滴积累,我的一小步O(∩_∩)O~*/