题目描述 Description输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列。输入描述 Input Description 共两行,第一行一个字符串,表示树的先序遍历,第二行一个字符串,表示树的中序遍历。输出描述 Output Description仅一行,表示树的后序遍历序列。样例输入 Sample Inputabdehicfgdbheiafcg样例输出 Sample Outputdhiebfgca数据范围及提示 Data Size & Hint输入长度不大于255。 1 #include 2 #include 3 #include 4 5 using namespace std; 6 7 /* 8 s1=abdehicfg 9 s2=dbheiafcg10 */1112 string s1,s2;//定义输入的字符串 1314 void calc(int l1,int r1,int l2,int r2)15 {16 int m=s2.find(s1[l1]);17 /*18 从s2中寻找s1的第一个字符,样例中为a,返回5,m=519 此时l2为020 故m>l2,将l1=l1(0)+m(5)-l2(0)+1=6,r1不变,l2=m+1=5+1=6,r2不变;21 所以变成6,8,6,822 ………………23 最后输出24 */25 if(m>l2) calc(l1+1,l1+m-l2,l2,m-1);26 if(m1,r1,m+1,r2);27 cout<
来源: http://www.bubuko.com/infodetail-2002196.html