PAT_A 1119. Pre- and Post-order Traversals (30)
来源:互联网 发布:淘宝网开店 编辑:程序博客网 时间:2024/06/14 04:20
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
分析
考二叉树的几种遍历方式code
#include<iostream>#include<vector>using namespace std;#define N 3577struct _tree{ int left; int right; int value;};int pre[N];int post[N];_tree tree[N];bool unique=true;vector<int> in;void inTrave(int child){ if(child==0) { return; }// in.push_back(child); inTrave(tree[child].left); in.push_back(child); inTrave(tree[child].right);}void deTrave(int preBegin,int preEnd,int postBegin,int postEnd,int root){ if(preBegin>preEnd) return; if(postBegin>postEnd) return; if(pre[preBegin]==post[postEnd]) { unique=false; tree[root].value=root; tree[root].left=pre[preBegin]; deTrave(preBegin+1,preEnd,postBegin,postEnd-1,pre[preBegin]); } else { tree[root].value=root; tree[root].left=pre[preBegin]; tree[root].right=post[postEnd]; int i=0; int j=0; for(i=postBegin;i<=postEnd;i++) { if(pre[preBegin]==post[i]) break; } for(j=preBegin;j<=preEnd;j++) { if(post[postEnd]==pre[j]) break; } //匹配左侧 deTrave(preBegin+1,j-1,postBegin,i-1,pre[preBegin]); deTrave(j+1,preEnd,i+1,postEnd-1,post[postEnd]); }}int main(){ int count=0; cin>>count; //pre[0]=count; for(int i=1;i<=count;i++) { cin>>pre[i]; pre[0]++; } post[0]=count; for(int i=1;i<=count;i++) { cin>>post[i]; } if(count==0) return 0; deTrave(2,pre[0],1,post[0]-1,pre[1]); if(unique==true) cout<<"Yes"<<endl; else cout<<"No"<<endl; inTrave(pre[1]); int i=0; for(i=0;i<in.size()-1;i++) { cout<<in.at(i)<<" "; } cout<<in.at(i)<<endl; return 0;}/* * 题目说明: * * * 考察二叉树的遍历问题 * NLR,LRN,LNR之间的关系,以及对三种遍历的深刻理解 * NLR LRN 如果我们将 LR RL视为一组即都是他们的孩子,则此时NLR LRN是对称的 * 一般我们对遍历前序能很快写出来,同样对于后序,我们从右侧,把R当做L,L当做R,那么此时 * 后序的也就能很快书写出来。 * 通过NLR和LRN我们能找到共同的N,并找到它的左右孩子,当它没有左右孩子时,解递归到那一层时, * 就只剩下该节点了。 * 接着我们要在N节点下的左右孩子下分别独立的递归下去,直到其中一个没有数据 * * * NLXXXXXXXRXXXXXXX * YYYYLYYYYYYYYYYYRN * * * 左子树递归部分 * LXXXXXXXXXXXXXXX * YYYYL * 右子树递归部分 * RXXXXXXX * YYYYLYYYYYYYYYYYR * . * . * . * * 这其实是对我们遍历时递归的一个解递归 * * * 关于二义性问题 * * 每一个递归部分 * 我们通过NLR的L该值为x * LRN的R值为y, * 通过x,y确定N的左右孩子 * 当x==y时,我们无法确定该值是左还是右 * */
- PAT_A 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals
- PAT--1119. Pre- and Post-order Traversals
- 1119. Pre- and Post-order Traversals
- (PAT)1119. Pre- and Post-order Traversals
- Android gradle实用教程(一):Android studio中的gradle
- linux环境下的vi编辑器详解(一)
- jquery中事件
- android中xml文件的作用及解释
- LinearRegressionWithSGD 问题
- PAT_A 1119. Pre- and Post-order Traversals (30)
- Poj 2785 4 Values whose Sum is 0【二分】
- 算法第四版3-2Binary Search Trees With C++ Implementation
- Intent概括
- 编写简单的内核模块——Linux操作系统原理与应用(陈莉君第2版13页)
- 使用排序算法对数组进行排序,按升序 注:数组内容只会有大写字母,且可能会出现重复,不允许使用java.util下的任何类
- 优雅编程之这样使用枚举和注解,你就“正常”了(二十九)
- 弱校连萌 2016 国庆训练计划 10,5 Problem A. As Easy As Possible
- Bias和Variance