文章
5
粉丝
0
获赞
21
访问
390
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char data;
struct node *left;
struct node *right;
} Node;
Node *build_tree (char *pre, char *in, int n) {
if (n <= 0) return NULL;
Node *root = (Node *)malloc(sizeof(Node));
root->data = pre[0];
int idx = 0;
for (int i = 0; i < n; i++) {
if (in[i] == pre[0]) {
idx = i;
break;
}
}
root->left = build_tree(pre + 1, in, idx);
root->right = build_tree(pre + idx + 1, in + idx + 1, n - idx - 1);
return root;
}
void post_order (Node *root) {
if (root == NULL) return;
post_order(root->left);
post_order(root->right);
printf("%c", root->data);
}
int main () {
char pre[27];
char in[27];
while (scanf("%s", pre) != EOF) {
scanf("%s", in);
Node *root = build_tree(pre, in, strlen(pre));
post_order(root);
printf("\n");
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发