PAT甲级1119

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

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Special
作者
CHEN, Yue

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

#include <cstdio>
#include <vector>
using namespace std;
vector<int> ans;
int *pre, *post, unique = 1;

int findFromPre (int x, int l, int r) {
	for (int i = l; i <= r; i++) {
		if (x == pre[i]) {
			return i;
		}
	}
	return -1;
}

void setIn (int prel, int prer, int postl, int postr) {
	if (prel == prer) {
		ans.push_back(pre[prel]);
		return;
	}
	if (pre[prel] == post[postr]) {
		int x = findFromPre(post[postr - 1], prel + 1, prer);
		if (x - prel > 1) {
			setIn(prel + 1, x - 1, postl, postl + x - prel - 2);
			ans.push_back(post[postr]);
			setIn(x, prer, postl + x - prel - 2 + 1, postr - 1);
		} else {
			unique = 0;
			ans.push_back(post[postr]);
			setIn(x, prer, postl + x - prel - 2 + 1, postr - 1);
		}
	} 
}

int main() {
	int n = 0;
	scanf("%d", &n);
	pre = new int [n];
	post = new int [n];
	for (int i = 0; i < n; i++) {
		scanf("%d", &pre[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &post[i]);
	}
	setIn(0, n - 1, 0, n - 1);
	printf("%s\n", unique ? "Yes" : "No");
	printf("%d", ans[0]);
	for (int i = 1; i < ans.size(); i++) {
		printf(" %d", ans[i]);
	}
	printf("\n");
	return 0;
}
#include <iostream>  
using namespace std;
const int maxn = 31;

int n, index = 0;
int pre[maxn], post[maxn];
bool flag = true;

struct Node {
	int data;
	Node *lchild, *rchild;
} *root;

Node *create(int preL, int preR, int postL, int postR)
{
	if (preL > preR) return NULL;//不合理就返回NULL
	Node *node = new Node;//建立根结点
	node->data = pre[preL];
	node->lchild = NULL;
	node->rchild = NULL;
	if (preL == preR)
		return node;//若只有一个节点那么直接返回即可
	int k = 0;
	for (k = preL + 1; k <= preR; k++)
	{
		if (pre[k] == post[postR - 1]) break;//从前序中找后序中子树根的位置
	}
	if (k - preL > 1)
	{
		node->lchild = create(preL + 1, k - 1, postL, postL + k - preL - 2);
		node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
	}
	else
	{
		flag = false;
		node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
	}
	return node;
}

void inOrder(Node *node)
{
	if (node == NULL) return;
	inOrder(node->lchild);
	if (index < n - 1)
		cout << node->data << " ";
	else cout << node->data << endl;
	index++;
	inOrder(node->rchild);
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; ++i) cin >> pre[i];
	for (int i = 0; i < n; ++i) cin >> post[i];
	root = create(0, n - 1, 0, n - 1);
	if (flag) cout << "Yes\n";
	else cout << "No\n";
	inOrder(root);
	return 0;
}
/*
这题我开始做有点难以理解,后来想清楚了:我解释下
【1】 2 3 4 6 7 5
 2 6 7 4 5 3 【1】
 一定要记住,前序是根 左 右的
 遍历顺序而后序是左 右 根的遍
 历顺序那么通过确定后序中3在前
 序中的位置,那么就知道如何划分
 子树了,因为3一定是1的右子树的
 根,那么在前序中,3之前1之后
 的序列就是左子树,3到5之间都
 为右子树的一部分,再递归处理即可
 但是若根结点只有一颗子树,那么
 得确定到底是左还是右,自己决定
 这也是前+后不一定能唯一确定树的原因
 1 2 3 4
 2 4 3 1
 1为根,找右子树的根即3在前序中的位置
 那么2是左子树,3 4是右子树,继续递归
 2只有一个直接返回,3 4中3是根,4是子
 树,但是左还是右?自己决定,这种情况就是不能唯一确定
*/


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值