国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT甲級1119

2019-11-08 19:24:33
字體:
來源:轉載
供稿:網友

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:
71 2 3 4 6 7 52 6 7 4 5 3 1Sample Output 1:
Yes2 1 6 4 7 3 5Sample Input 2:
41 2 3 42 4 3 1Sample Output 2:
No2 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是子 樹,但是左還是右?自己決定,這種情況就是不能唯一確定*/
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西充县| 乡城县| 抚宁县| 建阳市| 天峨县| 马鞍山市| 营口市| 宁远县| 绿春县| 仙桃市| 大邑县| 天祝| 罗平县| 宜兰市| 林口县| 上犹县| 新安县| 武威市| 鞍山市| 项城市| 炎陵县| 辽宁省| 阿瓦提县| 天峻县| 蒙阴县| 香港 | 双城市| 镇沅| 兴国县| 腾冲县| 孙吴县| 米易县| 勐海县| 岐山县| 托克托县| 漳州市| 松原市| 卓资县| 辽阳县| 夏河县| 湖北省|