1119 Pre- and Post-order Traversals (30 分)

2月4号:update版本~

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

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

题目大意:给一个先序序列和后序序列,问是否能唯一确定一颗二叉树,若能唯一确定就输出它的中序序列,否则就输出其中一组中序序列。

分析:一般来说确定一颗二叉树需要:先序/后序+中序的组合,或者光完全二叉树的层次遍历也可以。

针对后序+先序组合:我们知道~后序的最后一个结点和先序的第一个肯定是根节点。如果根节点左右孩子都存在,那么后序的倒数第二个一定是根节点的右子树(能确定左右子树的情况);若根节点只有左子树或者右子树-----转化为查先序数组,第二个元素与后序倒数第二个元素相同,这就是一种无法确定的状态了,此时看作右子树处理。然后递归判断左右子树即可。

不能唯一确定二叉树的情况-----二叉树中存在子树只有左子树或者右子树。

1)建树版本:
AC代码:

//建树版本
#include<bits/stdc++.h>
using namespace std;
int pre[50],post[50];
struct node{
	int val;
	node *l,*r;
};

node *creat(int prel,int prer,int postl,int postr){
	if(prel>prer) return NULL;
	node *root=new node;
	root->val =post[postr];
	int k;
	//找根的右子树,若只有单孩子,当右子树处理 
	for(k=prel+1;k<=prer;k++){
		if(pre[k]==post[postr-1]) break; 
        }
        root->l =creat(prel+1,k-1,postl,postl+k-1-(prel+1));
        root->r =creat(k,prer,postr-1-(prer-k),postr-1); 
        return root;
}

bool dfs(node *root){
	if(!root) return true;
	if(root->r && (!root->l )) return false;
	return dfs(root->l )&& dfs(root->r );
}
int cnt=0;
void inorder(node *root){
	if(root){
		inorder(root->l );
		cnt++;
		printf("%s%d",cnt==1?"":" ",root->val );
		inorder(root->r );
	}
} 
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++) cin>>pre[i];
	for(int i=0;i<n;i++) cin>>post[i];
	node *root=creat(0,n-1,0,n-1);
	if(dfs(root)) cout<<"Yes\n";
	else cout<<"No\n";
	inorder(root);
	cout<<endl;
	return 0;
} 

2)不建树版本:

对建树版本稍加改造即可AC

#include<bits/stdc++.h>
using namespace std;
int pre[50],post[50];
bool flag=true;
vector<int>inorder;
void creat(int prel,int prer,int postl,int postr){
	if(prel>prer) return ;
	int k;
	for(k=prel+1;k<=prer;k++){
		if(pre[k]==post[postr-1]) break; 
        }
        if(pre[prel+1]==post[postr-1] && prel+1<=prer) flag=false;
        //此处应该加上越界判断,否则只有一个节点的二叉树测试点5过不了
        creat(prel+1,k-1,postl,postl+k-1-(prel+1));
	inorder.push_back(pre[prel]);
        creat(k,prer,postr-1-(prer-k),postr-1); 
}

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++) cin>>pre[i];
	for(int i=0;i<n;i++) cin>>post[i];
	creat(0,n-1,0,n-1);
	printf("%s\n%d",flag?"Yes":"No",inorder[0]);
	for(int i=1;i<n;i++) cout<<" "<<inorder[i];
	cout<<endl;
	return 0;
}

END!

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZCAIHUI_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值