这里我们用递归的方法,对二叉树进行一个建立的操作。,然后后序遍历输出即可。
#include <bits/stdc++.h>
using namespace std;
string s1, s2;
struct node{
int l, r;
char val;
} tr[100000];
int dfs(int al, int ar, int bl, int br){
tr[al].val = s1[al];
if (al > ar) return al;
int p = s2.find(s1[al]);
if (al + 1 <= al + p - bl && bl <= p - 1)
tr[al].l = dfs(al + 1, al + p - bl, bl, p - 1);
if (al + p - bl + 1 <= ar && p + 1 <= br)
tr[al].r = dfs(al + p - bl + 1, ar, p + 1, br);
return al;
}
void print(int x){
if (tr[x].l)
print(tr[x].l);
if (tr[x].r)
print(tr[x].r);
cout << tr[x].val;
}
int main() {
cin >> s1 >> s2;
int n = s1.size();
s1 = " " + s1, s2 = " " + s2;
dfs(1, n, 1, n);
print(1);
}