题目描述 Description
给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度<=8)。
输入描述 Input Description
两个字符串,分别是中序和后序(每行一个)
输出描述 Output Description
一个字符串,先序
样例输入 Sample Input
BADC
BDCA
样例输出 Sample Output
ABCD
数据范围及提示 Data Size & Hint
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; string zx,hx; void get_hx(int l1,int r1,int l2,int r2) { int m=zx.find(hx[r2]); cout<<hx[r2]; if(m>l1) get_hx(l1,m-1,l2,l2+m-1-l1); if(m<r1) get_hx(m+1,r1,l2+m-l1,r2-1); } int main() { cin>>zx>>hx; int r1=zx.length()-1; int r2=hx.length()-1; get_hx(0,r1,0,r2); return 0; }