PAT A1119 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

大体题意

总所周知,前序和后序是不能唯一地确定一棵树的,其真正的意思是:并不是所有的前序和后序都能确定一颗树。但其还是存在前序和后序可以确定一棵树的情况。
题目就是让你判断所给的前序和后序能不能唯一的确定一棵树
如果可以,输出它的中序序列,如果不可以,输出其中序序列的其中一个可能。

思路

首先确定,什么情况下前序和后序不能确定一棵树。
在这里插入图片描述
由图中的结论可以知道:当根只有一个孩子时,或者说,假设唯一的孩子是右孩子,根没有左孩子时(这时真正的情况是不知道该孩子是左孩子还是右孩子)
题目要求当不唯一时输出一种情况,因此就假设唯一的孩子是右孩子
那么就以这个右孩子为分界,将结点分为左右子树:
在这里插入图片描述
由图所得,区分左右子树的步骤为:
1.确定根,以后序根的前一个结点为分界,找到它在前序中的位置标为i
2.前序中结点i的前面的所有结点都是左子树,后面所有的结点(包括自身)都是右子树
3.根据前序在后序序列中划分即可
实现二叉树不唯一的判断:
在这里插入图片描述
在前序序列中右孩子和根之间没有其余结点:i-preL<1,则结果不唯一
判断二叉树是否唯一在哪一部分执行比较好?
在左子树遍历的部分,判断若i-pre>1,说明左子树存在,进行左子树的遍历,若不满足则左子树不存在,将标志改变且不进行左子树的遍历即可
无需判断右子树是否存在,若右子树不存在则左子树一定不存在,此时只有一个根,此时直接添入中序vector中即可

代码

/*
给定二叉树的先序遍历和后序遍历,要求输出其中序遍历,不唯一输出任意一个即可

二叉树的先序和后序是无法唯一确定一棵二叉树的
因为可能会存在多种情况,这种情况就是一个结点可能是根的左孩子也可能使根的右孩子
因为题目只需要输出一个方案,可以假定这个不可确定的孩子的状态是右孩子

设置标志符unique来标志是否有多重情况
接着就是如何将先序和后序转换为中序的问题了
需要四个变量,preL,preR,postL,postR
前序的开始第一个应该是后序的最后一个,这个节点就是根结点
以后序的根结点的前面一个结点作为参考,寻找这个节点在前序的位置i
getIn(preL + 1, i - 1, postL, postL + (i - preL - 1) - 1);//左子树
注意中间要将根结点加入到in的容器中(因为是中序遍历)
getIn(i, preR, postL + (i - preL - 1), postR - 1);//右子树
就可以根据这个位置来划分左右孩子
如果(i - preL > 1)则当前没有多重情况,否则要置unique为false
*/

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

//分别用来保存中序、前序、后序序列
vector<int> in, pre, post;
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])
	{
		//找到post[postR-1]在前序中的位置
		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;//二叉树不唯一,标志为false
		in.push_back(post[postR]);//根
		getIn(i, preR, postL + (i - preL - 1), postR - 1);//右
	}
}

int main()
{
	int n;
	cin >> n;
	int x;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		pre.push_back(x);
	}
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		post.push_back(x);
	}
	getIn(0, n - 1, 0, n - 1);
	if (unique == true)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
	cout << in[0];
	for (int i = 1; i < in.size(); i++)
		cout << " " << in[i];
	cout << endl;
}

运行结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值