码迷,mamicode.com
首页 > 其他好文 > 详细

PAT A1119 Pre- and Post-order Traversals (30 分)

时间:2021-02-16 12:28:45      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:distinct   实现   慢慢   HERE   固定   for   ext   cas   完全   

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

实现思路:

题意是根据先序和后序建树,并且判断所建立的树是否唯一。
根据先序TLR和后序LRT的顺序可以发现先序可以固定根节点,然后在建立根节点后,如果递归建树,需要划分左右孩子结点的个数,这点是解题关键,如果通过画图发现如果要计算左孩子结点数要通过根节点右孩子结点来计算,因为在后序遍历中,当前根节点root的右孩子结点遍历的位置是处于根节点左边的第一个结点,先序TLR,后序LRT所以后序中R是在T左边的第一个结点的,所以在先序序列中找到和后序序列中R这个位置的数据相同的元素时便可以算到左孩子树的结点个数了,这里会有点绕,建议把本题例题中例子画一棵树对照文字理解即可,画图不方便请谅解,想看图的建议去算法笔记对应本题有很详细的图。
本题还有一个关键处:判断树是否唯一的,通过第二个例子和第一个样例数据对比可知道,不唯一的情况就是当先序序列pre[preL+1]==post[post-1]的时候,那么建立出来的树就是不唯一的。

AC代码:

#include <iostream>
using namespace std;
const int N=50;
int pre[N],post[N],n;
struct node {
	int data;
	node *l,*r;
};

bool isOnly=true;
node* build(int preL,int preR,int postL,int postR) {
	if(preL>preR) return NULL;
	node *root=new node;
	root->data=pre[preL];
	int cnt=0;
	if(postR-1>=0&&preL+1<=preR) {
		if(pre[preL+1]==post[postR-1]) isOnly=false;
		for(int i=preL+1; i<=preR; i++) {
			if(pre[i]==post[postR-1]) break;
			cnt++;//统计左孩子个数
		}
	}
	root->l=build(preL+1,preL+cnt,postL,postL+cnt-1);
	root->r=build(preL+cnt+1,preR,postL+cnt,postR-1);
	return root;
}

int num=0;
void inorder(node *root) {
	if(root==NULL) return;
	inorder(root->l);
	printf("%d",root->data);
	num++;
	if(num<n) printf(" ");
	else printf("\n");
	inorder(root->r);
}

int main() {
	cin>>n;
	for(int i=0; i<n; i++) scanf("%d",&pre[i]);
	for(int i=0; i<n; i++) scanf("%d",&post[i]);
	node *root=build(0,n-1,0,n-1);
	if(isOnly) printf("Yes\n");
	else printf("No\n");
	inorder(root);
	return 0;
}

小结: 这题是第二次做了,第一次做还是很久前了应该,这次完全没有借鉴是通过自己画图慢慢摸索规律然后写出代码的,一次就AC了,个人感觉做PAT题库,或者考试也好,尽量需要多动笔,先把题目规律理清楚,然后再写代码,起始PAT没有多大难度(勿喷,个人是对比ACM和NOI一些难度),无非是考察规律的发掘和逻辑,然后是否可以通过代码去实现,画图理解规律了,PAT应该就可以拿到高分。

PAT A1119 Pre- and Post-order Traversals (30 分)

标签:distinct   实现   慢慢   HERE   固定   for   ext   cas   完全   

原文地址:https://www.cnblogs.com/coderJ-one/p/14399617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!