Problem : 恢复一棵树

Problem : 恢复一棵树

Time Limit: 1 Sec Memory Limit: 128 MB

Description

给出树的前序遍历及中序遍历,求其后序遍历。

Input

存在多组数据,请做到文件底结束
每组数据给出两个字符串,均不超过26个字母。分为前序、中序遍历。

Output

如题

Sample Input

DBACEGF ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB
代码如下

#include<bits/stdc++.h>
std::string s,ss;
void tree(int f,int u,int c,int k) {
    if(f>u) return;
    int m=ss.find(s[f]);
    tree(f+1,f+m-c,c,m-1);
    tree(f+m-c+1,u,m+1,k);
    std::cout<<s[f];
}
int main() {
    while(std::cin>>s>>ss)
        tree(0,s.length()-1,0,ss.length()-1),std::cout<<std::endl;
}
posted @ 2019-01-30 16:03  ZhaoChongyan  阅读(82)  评论(0编辑  收藏  举报