hihocoder 1049 后序遍历树

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<cmath>
#define L 2000050
using namespace std;

char pre[30],in[30],post[30];

int n = 0;
void dfs(char *pre,char *in)//知道前序遍历,中序遍历,求后序
{

    if(*in == 0) return;
    char *t = in;
    int len = 0;
    while(*t++ != *pre) len++;  //根据根在中序遍历中的位置,划分左右子树
    in[len] = 0;
    dfs(pre+1,in);//遍历左子树
    dfs(pre+len+1,in+len+1);//遍历右子树
    post[n++] = *pre;//
}

int main(){

   // freopen("1.txt","r",stdin);
    scanf("%s %s",pre,in);

    dfs(pre,in);
    post[n] = 0;
    puts(post);
    return 0;
}

 

posted on 2015-11-20 14:54  zyz913614263  阅读(166)  评论(0编辑  收藏  举报

导航