复习题之求后序遍历

2010 求后序遍历

 

 时间限制: 1 s
 空间限制: 64000 KB
 题目等级: 白银 Silver
 
 
题目描述 Description

输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列。

输入描述 Input Description

共两行,第一行一个字符串,表示树的先序遍历,第二行一个字符串,表示树的中序遍历。

输出描述 Output Description

仅一行,表示树的后序遍历序列。

样例输入 Sample Input

abdehicfg

dbheiafcg

样例输出 Sample Output

dhiebfgca

数据范围及提示 Data Size & Hint

输入长度不大于255。

分类标签 Tags 

递归 搜索
 
/*我知道自己的函数名很好看……*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
string s1,s2;
void o_o(int la,int ra,int lb,int rb)
{
    int m=s2.find(s1[la]);
    if(m>lb)o_o(la+1,la+m+lb,lb,m-1);
    if(m<rb)o_o(la+m-lb+1,ra,m+1,rb);
    cout<<s1[la];
}
int main()
{
    cin>>s1>>s2;
    o_o(0,s1.length()-1,0,s2.length()-1);
    return 0;
}
原文地址:https://www.cnblogs.com/EvilEC/p/6856393.html