首页 > 其他 > 详细

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

时间:2020-05-09 17:27:14      阅读:43      评论:0      收藏:0      [点我收藏+]
#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;
}

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

原文:https://www.cnblogs.com/Accpted/p/12858757.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!