1119 Pre- and Post-order Traversals

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

题目大意

给出一棵二叉树的前序遍历序列和后序遍历序列,判断能否得到唯一的树,若唯一,输出其中序遍历序列,若不唯一,输出其任意一个中序便利序列。

思路

先序遍历的顺序是:根左右,后序遍历的顺序是:左右根。也就是说,我们通过两序列可以找出该树(或子树)的根结点,但该根结点的左右子树是否存在以及结点数量无法确定。而当一边子树存在、一边不存在时,我们无法确定是哪边不存在,也就会导致解的不唯一。
所以,我们在构建中序序列时,首先要做的就是确定该根结点对应左子树和右子树的结点数量,设左子树结点数量为cnt。由于任意子树先序序列中根结点位于第一个,后序序列中根结点位于最后一个,在后序序列中找到该左子树根结点的位置,便能得出cnt,从而得出左右子树两个序列的起始及结束序号。若cnt为该树结点数-1,则无法判断该子树是否为左子树(也可能是右子树,我们默认它是左子树),即有多个解。

AC代码

#include <iostream>
using namespace std;

const int maxn = 31;
int n, index = 0, pre[maxn], in[maxn], post[maxn];
bool flag = true; //是否唯一 

void Build(int l1, int h1, int l2, int h2){
	if(l1 >= h1){
		if(l1 == h1) //到达叶子结点
			in[index++] = pre[l1];
		return; 
	}
	int cnt = 0; //确定左子树结点数量
	while(pre[l1 + 1] != post[l2 + cnt])
		cnt++; //找到左子树根结点在后序序列中的位置 
	cnt++;
	if(cnt == h1 - l1) flag = false; //只有一个子树
	Build(l1 + 1, l1 + cnt, l2, l2 + cnt - 1);
	in[index++] = pre[l1];
	Build(l1 + cnt + 1, h1, l2 + cnt, h2 - 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];
	Build(0, n - 1, 0, n - 1);
	if(flag) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	for(int i = 0; i < n; i++){
		if(i != 0) cout<<" ";
		cout<<in[i];
	}
	cout<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值