二叉树之已知前序和中序遍历求后序遍历(POJ2255 &&HDU )

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;
typedef long long LL;
const int N=100+10;

char pre[N],in[N],last[N];

void Find_Last(int l1,int r1,int l2,int r2) {
    if (l1 > r1) return;
    int root = l2;
    while (in[root] != pre[l1]) {
        root++;
    }
    Find_Last(l1 + 1, l1 + root - l2, l2, root - 1);
    Find_Last(l1 + root + 1 - l2, r1, root + 1, r2);
    printf("%c", pre[l1]);
}
int main() {
    while (scanf("%s%s", pre, in) == 2) {
        int len = strlen(pre) - 1;
        Find_Last(0, len, 0, len);
        puts("");
    }
    return 0;
}
posted @ 2020-05-09 16:59  Snow_in_winer  阅读(156)  评论(0编辑  收藏  举报