求后序遍历x

题目描述 Description

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

输入描述 Input Description

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

输出描述 Output Description

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

样例输入 Sample Input

abdehicfg

dbheiafcg

样例输出 Sample Output

dhiebfgca

数据范围及提示 Data Size & Hint

输入长度不大于255。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 string s1,s2;
 8 
 9 void calc(int l1,int r1,int l2,int r2) {
10     int m=s2.find(s1[l1]);
11     if(m>l2) calc(l1+1,l1+m-l2,l2,m-1);
12     if(m<r2) calc(l1+m-l2+1,r1,m+1,r2);
13     cout<<s1[l1];
14 }
15 
16 int main() {
17     cin>>s1>>s2;
18     calc(0,s1.length()-1,0,s2.length()-1);
19     cout<<endl;
20     return 0;
21 }

如果运气好也是错,那我倒愿意错上加错!

❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀

原文地址:https://www.cnblogs.com/zxqxwnngztxx/p/6648007.html