大会员可查看代码,点此开通大会员
#include <stdio.h>
#include <stdlib.h>
static int ko=0;
typedef struct node{
char val;
struct node *LCh;
struct node *RCh;
}Node,*BTree;
BTree CreatBtree(char ch[], BTree mt){
if (ch[ko]!='\0'){
if (ch[ko]=='#'){
ko++;
return NULL;
}
mt = malloc(sizeof(Node));
mt->LCh = NULL;
mt->RCh = NULL;
mt->val = ch[ko++];
mt->LCh = CreatBtree(ch, mt->LCh);
mt->RCh = CreatBtree(ch, mt->RCh);
}
return mt;
}
void show(BTree mt){
if (mt!=NULL){
//printf("%c",mt->c);
show(mt->LCh);
printf("%c ",mt->val);
show(mt->RCh);
}
}
int main(){
char ch[100];
int i=0;
BTree mt=NULL;
while (scanf("%s",&ch)!=EOF){
if (ch[0]=='0') break;
ko=0;
mt = CreatBtree(ch,mt);
show(mt);
printf("\n");
}
return 0;
}