PAT(A) 1119 Pre- and Post-order Traversals (30)(数据结构)

本文介绍了如何利用先序和后序遍历序列重建二叉树,重点在于理解根节点的确定及递归过程。

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

记录一个菜逼的成长。。

发现今年秋季的好简单。。亏了。

应该都写过知道中序和先序或者后序的一个然后重建树。

题目大意:
给你二叉树的先序和后序遍历序列。输出任意一种中序遍历序列(不唯一)。

知道先序和后序很容易知道根节点,然后从先序的根结点往后 和 后序序列的开头往后找按升序或降序排列后一样的序列,然后递归建树。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
#include <cassert>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a,b) memset(a,b,sizeof(a))
#define bp __builtin_popcount
#define pb push_back
#define mp make_pair
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
#define seglen (node[t].r-node[t].l+1)
#define pi 3.1415926
#define exp  2.718281828459
#define lowbit(x) (x)&(-x)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &x){
    T ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    x = ans;
}
const int maxn = 100 + 10;
map<int,int>tree;
int flag = 0;
int pre[maxn],post[maxn];
int vis[40];
void build( int t,int l1,int r1,int l2,int r2 )
{
    if(pre[l1] == post[r2]){
        tree[t] = pre[l1];
        if(l1 == r1)return ;
        int p;
        for( int len = 0,i = l1 + 1; i <= r1; i++,len++ ){
            cl(vis,0);
            for( int j = l1 + 1; j <= i; j++ ){
                vis[pre[j]] = 1;
            }
            int f = 1;
            for( int j = l2; j <= l2 + len; j++ ){
                vis[post[j]]--;
                if(vis[post[j]] < 0){
                    f = 0;
                    break;
                }
            }
            if(f){
                p = i;
                break;
            }
        }
        build(t<<1,l1+1,p,l2,p-l1+l2-1);
        build(t<<1|1,p+1,r1,p-l1+l2,r2-1);
    }
    else{
        flag = 1;
        tree[t<<1] = pre[l1];
        tree[t<<1|1] = pre[r1];
        return ;
    }
}
int f = 1;
void in_tra(int t)
{
    if(tree[t]){
        in_tra(t<<1);
        if(f)printf("%d",tree[t]),f = 0;
        else printf(" %d",tree[t]);
        in_tra(t<<1|1);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        for( int i = 0; i < n; i++ ){
            scanf("%d",pre+i);
        }
        for( int i = 0; i < n; i++ ){
            scanf("%d",&post[i]);
        }
        build(1,0,n-1,0,n-1);
        puts(flag ? "No" : "Yes" );
        in_tra(1);
        puts("");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值