hihoCoder week10 后序遍历

题目链接 https://hihocoder.com/contest/hiho10/problem/1

给出先序  中序 求 后序

#include <bits/stdc++.h>
using namespace std;

const int N = 100;

char a[N], b[N];

void divide(int sa,int ea,int sb,int eb)
{
    if(sa > ea || sb > eb)
        return ;
    int tmp = -1;
    for(int i=sb; i<=eb; i++)
        if(a[sa] == b[i]) {
            tmp = i;
            break;
        }
    int la = tmp - sb;
    // 递归左子树 
    divide(sa+1, sa+la, sb, tmp-1);
    // 递归右子树
    divide(sa+la+1, ea, tmp+1, eb);
    cout << a[sa];
}

int main()
{
    freopen("in.txt", "r", stdin);  
    scanf("%s %s", a, b);
    int len = strlen(a);
    divide(0, len-1, 0, len-1);
    return 0;
}
原文地址:https://www.cnblogs.com/Draymonder/p/9977222.html