先序遍历加中序遍历确定后序遍历
1 #define bug(x) cout<<#x<<" is "<<x<<endl 2 #define IO std::ios::sync_with_stdio(0) 3 #include <bits/stdc++.h> 4 using namespace std; 5 #define ll long long 6 #define pb push_back 7 const ll mod=1e9+7; 8 const int N=2e5+10; 9 10 char s[N],t[N]; 11 12 struct node{ 13 int c; 14 node *lson,*rson; 15 }; 16 17 int a[200],b[200],id[200],d[200],n,cnt; 18 19 node *root; 20 21 node* build(node *now){ 22 now =new node(); 23 //now=(node *)malloc(sizeof(node)); 24 //now->lson=now->rson=NULL; 25 int k=id[s[cnt]]; 26 d[k]=1; 27 now->c=s[cnt++]; 28 29 if(k>1&&!d[k-1]){ 30 now->lson=build(now->lson); 31 } 32 if(k<n&&!d[k+1]){ 33 now->rson=build(now->rson); 34 } 35 return now; 36 } 37 38 void preorder(node *now){ 39 if(now->lson!=NULL)preorder(now->lson); 40 if(now->rson!=NULL)preorder(now->rson); 41 printf("%c",now->c); 42 } 43 44 void work(){ 45 preorder(build(root)); 46 } 47 48 int main(){ 49 scanf("%s",s+1); 50 scanf("%s",t+1); 51 n=strlen(s+1); 52 53 for(int i=1;i<=n;i++){ 54 id[t[i]]=i; 55 } 56 work(); 57 } 58 /* 59 ABCD 60 BADC 61 BDCA 62 63 1 2 4 5 3 6 64 4 2 5 1 6 3 65 4 5 2 6 3 1 66 */