题目
【问题描述】采用先序遍历的顺序输入二叉树,度不为
2
的节点采用空格补全(
-
代表空
格),再分别采用先序、中序和后序算法对该二叉树进行遍历。
【输入形式】
12-46---3-5--
【输出形式】
【样例输入】
12-46---3-5--
【样例输出】
124635
264135
642531
#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode
{
struct BiTNode *lchild,*rchild;
char data;
} BiTNode,*BiTree;
void CreateB(BiTree *T)
{
char ch;
ch=getchar();
if(ch=='-')
{
(*T)=NULL;
}
else
{
(*T)=(BiTree)malloc(sizeof(BiTNode));
(*T)->data=ch;
CreateB(&(*T)->lchild);
CreateB(&(*T)->rchild);
}
}
void Preorder(BiTree T)
{
if(T==NULL)
{
return;
}
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
void Inorder(BiTree T)
{
if(T==NULL)
{
return;
}
Inorder(T->lchild);
printf("%c",T->data);
Inorder(T->rchild);
}
void Postorder(BiTree T)
{
if(T==NULL)
return;
Postorder(T->lchild);
Postorder(T->rchild);
printf("%c",T->data);
}
int main()
{
BiTree B;
CreateB(&B);
Preorder(B);
printf("\n");
Inorder(B);
printf("\n");
Postorder(B);
printf("\n");
return 0;
}