文章
19
粉丝
225
获赞
20
访问
53.3k
结束换行时得用'\0',用'\n'运行通不过,但是在自己的编辑器上是可以的.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct node {
char val;
struct node* left;
struct node* right;
};
int i = 0;
struct node* create(char s[], struct node* mt) {
if (s[i] != '\0') {
if (s[i] == '#') {
i++;
return NULL;
}
mt = (struct node*)malloc(sizeof(struct node));
mt->val = s[i++];
mt->left = create(s, mt->left);
mt->right = create(s, mt->right);
}
return mt;
}
void show(struct node* mt) {
if (mt != NULL) {
show(mt->left);
printf("%c ", mt->val);
show(mt->right);
}
}
int main() {
char s[100];
struct node* mt = NULL;
while (scanf("%s", s) != EOF) {
if (s[0] == '0')
break;
i=0;//这条语句很关键,每次都得使i=0才可
mt = create(s, mt);
show(mt);
printf("\n");
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发