3,881
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<malloc.h>
struct tree
{
char data;
struct tree *lchild,*rchild;
};
void creatTree(struct tree **t)
{
char ch;
scanf("%c",&ch);
if(ch=='*')
(*t)=NULL;
else
{
(*t)=(struct tree*)malloc(sizeof(struct tree));
(*t)->data=ch;
creatTree(&((*t)->lchild));
creatTree(&((*t)->rchild));
}
return;
}
void preorderPrint(struct tree *t)
{
if(!t) return;
printf("%c,",t->data);
preorderPrint(t->lchild);
preorderPrint(t->rchild);
}
int main()
{
struct tree *p;
creatTree(&p);
preorderPrint(p);
return 0;
}