536:Tree Recovery

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37754288/article/details/81517630

Tree Recovery

#include<bits/stdc++.h>
using namespace std;
const int maxn = 30;
struct node{
    char c;
    struct node* l = NULL;
    struct node* r = NULL;
};
char p[maxn],m[maxn];
node* build_tree(int i,int j,int k){
    node* root = new node;
    root->c = p[k];
    if(i == j) return root;
    int t = i;
    while(m[t] != p[k]) t++;
    if(t-1 >= i) root->l = build_tree(i,t-1,k+1);
    if(t+1 <= j) root->r = build_tree(t+1,j,k+t-i+1);
    return root;
}
void post_travel(node* root){
    if(!root) return;
    post_travel(root->l);
    post_travel(root->r);
    putchar(root->c);
}
int main(){
    // freopen("data.in","r",stdin);
    // freopen("data.out","w",stdout);
    while(scanf("%s %s",p,m) == 2){
        int len = strlen(p);
        node* root = build_tree(0,len-1,0);
        post_travel(root);
        putchar('\n');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37754288/article/details/81517630
今日推荐