二叉树的镜像

来源:互联网 发布:软件行业前景预测 编辑:程序博客网 时间:2024/05/16 12:42

#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;

}

原创粉丝点击