PAT甲级1119 Pre- and Post-order Traversals (30)

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

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

根据前序后序模拟树,看这个树是否唯一,如果唯一输出Yes,不唯一输出No,然后输出中序遍历,不唯一则输出其中一个中序遍历即可。

刚开始很迷糊,后来翻了很多博客讲解慢慢搞懂了,现在我详细的说一下 :

1.什么情况下后序和前序才不能确定一棵树呢?

比如题目给的第二个例子,我们把树画出来如下:

可以看到是两棵不同的二叉树,大家发现没,因为3结点只有一侧有孩子结点,也就是当该结点只有左子树或者右子树的时候才会发生不唯一(也就是度唯一)。(大家可以试着构造只有左子树或右子树的二叉树看看这个规律)

看题目第一个例子,如下图:

此二叉树没有度唯一的结点,就不存在不同的二叉树了。

2.只有左子树或右子树该如何在程序上判断并实现呢?(建议边看代码边看下面的解说)

还是根据第二个例子:

前序:1 2 3 4

后序:2 4 3 1

我们利用中序遍历的特点,结合递归,构造一个vector变量插入根节点。前序开头和后序结尾都是一样的为根节点,用后序倒数第二个数来做左右子树的划分,比如上述后序倒数第二个是3,那么在前序中找到3的位置,在前序遍历中,3前面除了根节点还有数,那么就表明1这个根节点有左子树,而3和后面的结点就只能充当右子树了,在有双子树的情况下继续遍历,按照递归顺序先递归到左子树:

前序:2

后序:2

只有2,按照递归边界插入2返回。之后按中序遍历顺序插入1,紧接着递归右子树:

前序:3 4

后序:4 3

这时注意,根据代码应该在前序中找4的位置,发现4与3(根节点)之间没有数字了,也就是说4既可以充当左子树也可以充当右子树了,此时情况不唯一,就把unique变量置为false,然后我们把不唯一的情况当作右子树遍历就产生其中一个中序遍历。因为没有遍历左子树了,直接插入3,之后遍历右子树:

前序:4

后序:4

根据递归边界插入4,返回,算法结束,输出中序遍历。

#include<iostream>
#include<vector>
using namespace std;

const int maxn=31;
int pre[maxn],post[maxn];
vector<int> in;
int n;
bool unique=true;

void getin(int prel,int prer,int postl,int postr){
	if(prel==prer){
		in.push_back(pre[prel]);
		return;
	}
	if(pre[prel]==post[postr]){
		//记i为前序中根的下一个结点
		int i=prel+1;
		//在前序中找到后序根结点前一个结点的位置来划分左右子树
		while(i<=prer&&pre[i]!=post[postr-1]) i++;
		if(i-prel>1){
			getin(prel+1,i-1,postl,postl+(i-prel-1)-1);
		}else unique=false;
		in.push_back(post[postr]);
		getin(i,prer,postl+(i-prel-1),postr-1);
	}
}

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>pre[i];
	}
	for(int i=0;i<n;i++){
		cin>>post[i];
	}
	getin(0,n-1,0,n-1);
	printf("%s\n%d", unique == true ? "Yes" : "No", in[0]);
	for (int i = 1; i < in.size(); i++)
		printf(" %d", in[i]);
	printf("\n");

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值