#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char a[55];
int i;
struct node
{
int data;
struct node *ls,*rs;
};
struct node *cr()
{
struct node *r;
char c;
c=a[i++];
if(c==',')
return NULL;
else
{
r=(struct node*)malloc(sizeof(struct node));
r->data=c;
r->ls=cr();
r->rs=cr();
}
return r;
};//建树
void p(struct node *r)
{
if(r==NULL)
return ;
else
{
printf("%c",r->data);
p(r->ls);
p(r->rs);
}
}//前
void f(struct node *r)
{
if(r==NULL)
return ;
else
{
f(r->ls);
printf("%c",r->data);
f(r->rs);
}
}//中
void q(struct node *r)
{
if(r==NULL)
return ;
else
{
q(r->ls);
q(r->rs);
printf("%c",r->data);
}
}//后
int main()
{
int k;
struct node *r;
while(scanf("%s",a)!=EOF)
{
i=0;
r=(struct node *)malloc(sizeof(struct node));
r=cr();
p(r);
printf("\n");
f(r);
printf("\n");
q(r);
printf("\n");
}
return 0;
}
标签:node,遍历,return,struct,rs,三种,二叉树,printf,ls From: https://blog.51cto.com/u_15879559/5868804