POJ2255 Tree Recovery 前序 中序求后序

本文详细介绍了如何根据前序和中序遍历的结果来找到二叉树的后序遍历序列。通过递归地分割问题,我们能够有效地重建树的结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据前序找到在中序的位置 其两端则是其左右节点

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include <queue>
#include <vector>
const int INF = 999999;
int len1,len2,len;
char qian[44],zhong[44],sum[44];
void dfs(int s1,int e1,int s2,int e2)
{
    if(s1>=e1)   return ;
    sum[--len]=qian[s1];
    int i;
    for( i=s2;i<e2;i++)
        if(qian[s1]==zhong[i])
            break;
    dfs(s1+i-s2+1,e1,i+1,e2);//右子树
    dfs(s1+1,s1+i-s2+1,s2,i);//左子树
}
int main()
{
   // freopen("in.txt","r",stdin);
   while(scanf("%s %s",qian,zhong)!=EOF)
   {
        len1=strlen(qian);
        len2=strlen(zhong);
        len=len1;
        sum[len]=0;
        dfs(0,len1,0,len2);
        printf("%s\n",sum);
   }
    return 0;
}


转载于:https://www.cnblogs.com/kewowlo/p/4002494.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值