#include <stdio.h> #include <string.h> void getPostOrder(char* preOrder, char* inOrder, int len) { if (len <= 0) { return; } char root = *preOrder;//将前序遍历序列的第一个字符赋值给变量root int rootIndex = 0; while (inOrder[rootIndex] != root) { rootIndex++; } getPostOrder(preOrder + 1, inOrder, rootIndex); getPostOrder(preOrder + rootIndex + 1, inOrder + rootIndex + 1, len - rootIndex - 1); printf("%c", root); } int main() { char preOrder[27], inOrder[27]; while (scanf("%s", preOrder) != EOF) { scanf("%s", inOrder); int len = strlen(preOrder); getPostOrder(preOrder, inOrder, len); printf("\n"); } return 0; }