PAT A1119 Pre- and Post-order Traversals

PAT A1119 Pre- and Post-order Traversals

在这里插入图片描述

Sample Input 1:

7
1 2 3 4 6 7 5
2 6 7 4 5 3 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4
  • 分析:先序后序建树:

>一般情况:左右子树都存在
就可以划分出左右子树:递归的在左右子树建树即可
Pre: R + [preL+1, preL+numL+1] + [preL + numL + 2, preR]
Post: [postL, idex] + [idex + 1, postR - 1] + R
所以:
lc = create(preL+1, preL+numL+1, postL, idex)
rc = create(preL + numL + 2, preR, idex + 1, postR - 1)
在这里插入图片描述
>ambiguous stituation:如果pre的第二个节点 与 post的倒数第二个节点相同,就无法判断这段区间是左是右(preo[preL+1] == posto[postR-1])
如果遇到这种情况:用一个标记位记录下,并假设是其中一种情况,如假设为左区间:
lc = create(preL + 1, preR, postL, postR - 1);
rc = NULL;
在这里插入图片描述

  • 思路 1:
  1. 先建树,并标记出是否是歧义情况
  2. 中序遍历
  • 坑点:
    最后额外要多输出一个\n

  • code 1:

#include <iostream>
#include <vector>
using namespace std;
const int maxn = 50;
int preo[maxn], posto[maxn];
struct node{
	int data;
	node *lc, *rc;
	node(){lc = rc = NULL;}
};
bool isUnique = true;
node* create(int preL, int preR, int postL, int postR){
	node* root = new node;
	root->data = posto[postR];	
	if(preL >= preR) return root;
	int idex = postL;
	while(idex <= postR && posto[idex] != preo[preL+1]) idex++;
	int numL = idex-postL;
	if(preo[preL+1] == posto[postR-1]){
		isUnique = false;
		root->lc = create(preL+1, preR, postL, postR-1);
		root->rc = NULL;
	}else{
		root->lc = create(preL+1, preL+numL+1, postL, idex);
		root->rc = create(preL+numL+2, preR, idex+1, postR-1);
	} 
	return root;
}
int cnt = 0;
void inOrder(node* r){
	if(r == NULL) return;
	inOrder(r->lc);
	if(cnt++ == 0)
		printf("%d", r->data);
	else printf(" %d", r->data);
	inOrder(r->rc); 
}
int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &preo[i]);
	}
	for(int i = 0; i < n; ++i){
		scanf("%d", &posto[i]);
	}
	node* rr = create(0, n-1, 0, n-1);
	printf("%s\n", isUnique ? "Yes" : "No");
	inOrder(rr);
	printf("\n");
	return 0;
} 
  • T2 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50;
int pre[maxn], post[maxn];
bool Unique = true;
struct node{
	int data;
	node *lc, *rc;
};
node* NewNode(int x){
	node* r = new node;
	r->data = x;
	r->lc = r->rc = NULL;
	return r;
} 
node* Create(int preL, int preR, int postL, int postR){
	node *r = NewNode(pre[preL]);
	if(preL >= preR){
		return r;
	} 
	if(preL < preR && pre[preL+1] == post[postR-1]){ 
		Unique = false;		
		r->lc = Create(preL + 1, preR, postL, postR - 1);
	}else{
		int id = 0;
		while(id < postR && post[id] != pre[preL + 1]) id++;
		int numL = id - postL + 1;
		r->lc = Create(preL + 1, preL + numL, postL, id);
		r->rc = Create(preL + numL + 1, preR, id + 1, postR - 1);
	}
	return r;
}
int cnt = 0;
void InOrder(node* r, int n){
	if(r == NULL) return;
	InOrder(r->lc, n);
	printf("%d", r->data);
	if(++cnt < n) printf(" "); 
	InOrder(r->rc, n);
}

int main(){
	int n;
	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]);
	}
	node* root = Create(0, n - 1, 0, n - 1);
	printf("%s\n", Unique ? "Yes" : "No");
	InOrder(root, n);
	printf("\n");
	return 0;
} 
  • 思路 2:
    不建树,通过先序后序求出左右子树区间,直接利用递归的原理(左 中 右)中序遍历

  • TIPS:
    注意递归边界条件:preL>=preR;模拟一下就可以看出来了,达到边界时要单独把边界这个点push进ans,在return上层

  • code 2:

#include <iostream>
#include <vector>
using namespace std;
const int maxn = 50;
int preo[maxn], posto[maxn];
vector<int> ans;
bool isUnique = true;
void inOrder(int preL, int preR, int postL, int postR){
	if(preL >= preR){
		ans.push_back(preo[preL]);
		return;
	}
	int idex = postL;
	while(idex < postR && posto[idex] != preo[preL+1]) idex++;
	int numL = idex - postL;
	if(preo[preL+1] != posto[postR-1]){	//1-
		inOrder(preL+1, preL+numL+1, postL, idex);
		ans.push_back(preo[preL]);
		inOrder(preL+numL+2, preR, idex+1, postR-1);
	}else{	//2-
		isUnique = false;
		inOrder(preL+1, preR, postL, postR-1);
		ans.push_back(preo[preL]);
	}
}
int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
		scanf("%d", &preo[i]);
	for(int i = 0; i < n; ++i)
		scanf("%d", &posto[i]);
	inOrder(0, n-1, 0, n-1);
	printf("%s\n", isUnique ? "Yes" : "No");
	for(int i = 0; i < ans.size(); ++i){
		if(i == 0) printf("%d", ans[i]);
		else printf(" %d", ans[i]);
	}
	printf("\n");	
	return 0;
}
  • T2 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50;
int pre[maxn], post[maxn];
bool Unique = true;
vector<int> in;
void Create(int preL, int preR, int postL, int postR){
	if(preL > preR) return;
	int r = pre[preL];
	if(preL < preR && pre[preL+1] == post[postR-1]){	//注意条件 preL < preR :得有孩子!! 
		Unique = false;
		in.push_back(r);
		Create(preL + 1, preR, postL, postR - 1);
	}else{
		int id = postR-1;
		while(id > postL && post[id] != pre[preL+1]) id--;
		int numL = id - postL + 1;
		Create(preL + 1, preL + numL, postL, id);
		in.push_back(r);
		Create(preL + numL + 1, preR, id + 1, postR - 1);
	}
}

int main(){
	int n;
	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]);		
	}
	Create(0, n-1, 0, n-1);
	printf("%s\n", Unique ? "Yes" : "No");
	for(int i = 0; i < in.size(); ++i){
		printf("%d", in[i]);
		if(i < in.size() - 1) printf(" ");
	}
	printf("\n");	//样例0, 1, 2, 3, 4, 6 
	return 0;
}
  • T3 code:
#include <bits/stdc++.h>
using namespace std;
vector<int> pre, post, in;
bool flg = true;
void Create(int preL, int preR, int postL, int postR)
{
    if(preL >= preR) return;
    int r = pre[preL];
    int numL = 0;  
    if(preL+1 < preR)   //必须提前预判:因为划分左右子树是通过左孩子,若左孩子不存在,划分都划不了
    {
        int lc = pre[preL+1], id = postL;
        if(lc == post[postR-2]) flg = false;
        while(id < postR && post[id] != lc) id++;
        numL = id - postL + 1;
    }
    Create(preL+1, preL+1+numL, postL, postL+numL);
    in.push_back(r);
    Create(preL+1+numL, preR, postL+numL, postR-1);
}
int main()
{
    int n;
    scanf("%d", &n);
    pre.resize(n); post.resize(n);
    for(int i = 0; i < 2; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            if(i == 0) scanf("%d", &pre[j]);
            else scanf("%d", &post[j]);
        }
    }
    Create(0, n, 0, n);
    printf("%s\n", flg ? "Yes" : "No");
    for(int i = 0; i < in.size(); ++i)
    {
        printf("%d", in[i]);
        if(i < in.size()-1) printf(" ");
    }
    printf("\n");
    return 0;
}

  • T4 code:
#include <bits/stdc++.h>
using namespace std;
vector<int> pre, post, in;
unordered_map<int, int> pos_post;
void Create(int preL, int preR, int postL, int postR, bool & isUnique)
{
    if(preL >= preR) return;   //有孩子的才能过滤下来,如:[1, 3)
    int r = pre[preL], numL = 0;
    if(preL + 1 < preR)    // [1, 2) ,1到了叶子节点
    {
        int lc = pre[preL+1], id = pos_post[lc];
        numL = id - postL + 1;
        if(id == postR - 2) isUnique = false;
    }
    Create(preL+1, preL+1+numL, postL, postL+numL, isUnique);
    in.push_back(r);
    Create(preL+1+numL, preR, postL+numL, postR-1, isUnique);
}
int main()
{
    int n;
    scanf("%d", &n);
    pre.resize(n); post.resize(n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%d", &pre[i]);
    }
    for(int i = 0; i < n; ++i)
    {
        scanf("%d", &post[i]);
        pos_post[post[i]] = i;
    }
    bool IS_UNIQUE = true;
    Create(0, n, 0, n, IS_UNIQUE);
    printf("%s\n", IS_UNIQUE ? "Yes" : "No");
    for(int i = 0; i < in.size(); ++i)
    {
        printf("%d", in[i]);
        if(i < in.size()-1) printf(" ");
    }
    printf("\n");
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值