#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct BNodeTree {
char data;
struct BNodeTree* left;
struct BNodeTree* right;
}BNodeTree;
BNodeTree* build_tree(char preorder[], char inorder[], int i) {
if(i == 0) return NULL;
BNodeTree* head = (BNodeTree*)malloc(sizeof(BNodeTree));
head->data = preorder[0];
char *p = strchr(inorder, preorder[0]);
int j = p - inorder;
head->left = build_tree(&preorder[1], inorder, j);
head->right = build_tree(&preorder[j+1], p+1, i-j-1);
return head;
}
void print_post_order(BNodeTree* head) {
if(!head) return;
print_post_order(head->left);
print_post_order(head->right);
printf("%c", head->data);
}
int main() {
char preorder[26], inorder[26];
int i, j;
while(EOF != scanf("%s%s", preorder, inorder)) {
i = strlen(preorder);
BNodeTree* head = build_tree(preorder, inorder, i);
print_post_order(head);
printf("\n");
}
return 0;
}
二叉树前序中序输出后序
最新推荐文章于 2024-10-18 08:00:00 发布