#include<stdio.h>
#include<stdlib.h>
struct tree
{
char data;
struct tree *lchild;
struct tree *rchild;
};
void creat(struct tree** T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')*T=NULL;
else
{
*T=(struct tree*)malloc(sizeof(struct tree));
(*T)->data=ch;
creat(&(*T)->lchild);
creat(&(*T)->rchild);
}
}
void pretravese(struct tree **T)
{
if(*T!=NULL)
{
printf("%c",(*T)->data);
pretravese(&(*T)->lchild); pretravese(&(*T)->rchild);
}
}
void main()
{
struct tree *root;
creat(&root);
pretravese(&root);
printf("/n");
}
运行结果:
12##3##
123
Press any key to continue
struct tree
{
char data;
struct tree *lchild;
struct tree *rchild;
};
void creat(struct tree** T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')*T=NULL;
else
{
*T=(struct tree*)malloc(sizeof(struct tree));
(*T)->data=ch;
creat(&(*T)->lchild);
creat(&(*T)->rchild);
}
}
void pretravese(struct tree **T)
{
if(*T!=NULL)
{
printf("%c",(*T)->data);
pretravese(&(*T)->lchild); pretravese(&(*T)->rchild);
}
}
void main()
{
struct tree *root;
creat(&root);
pretravese(&root);
printf("/n");
}
运行结果:
12##3##
123
Press any key to continue