运行环境:VS2017社区版
#include "stdafx.h"
#include"stdio.h"
//定义树节点
typedef struct BiTNode
{
int Value;
BiTNode* pLeft;
BiTNode* pRight;
}BiTNode, *BiTree;
//定义镜像函数
void Mir(BiTNode* T)
{
if (T)
{
BiTNode* pTemp =NULL;
if (T->pLeft !=NULL || T->pRight != NULL)
{
pTemp = T->pLeft;
T->pLeft =T->pRight;
T->pRight = pTemp;
}
if (T->pLeft !=NULL)
{
Mir(T->pLeft);
}
if (T->pRight !=NULL)
{
Mir(T->pRight);
}
}
return;
}
//以先序遍历的方式创建二叉树
void Create(BiTree &T)
{
char c;
scanf_s("%c",&c);
if ('#' == c)
{
T = NULL;
}
else
{
T = new BiTNode();
T->Value = c;
Create(T->pLeft);
Create(T->pRight);
}
return ;
}
//打印
void Print(BiTNode*T)
{
if (T)
{
printf("%c",T->Value);
Print(T->pLeft);
Print(T->pRight);
}
return;
}
int main()
{
BiTNode* T=NULL;
printf("请输入先序序列创建的二叉树:\n");
Create(T);
printf("请先序输出二叉树:\n");
Print(T);
printf("\n");
printf("请输出镜像:\n");
Mir(T);
Print(T);
printf("\n");
return 0;
}