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

来源:互联网 发布:怎样修改游戏数据 编辑:程序博客网 时间:2024/05/19 08:46

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 1
Sample Output 1:
Yes2 1 6 4 7 3 5
Sample Input 2:
41 2 3 42 4 3 1
Sample Output 2:
No2 1 3 4
由于对树的遍历序列缺乏深入了解,所以用了费力不讨好的穷举方式,模仿前(后)序和中序序列建树的方式,对剩下部分进行集合划分,来穷举所有情况。但为了保留一个解,所以找到一个解和判断解是否唯一用了两次建树(暴露智商),因为不知道在递归建树的过程中能不能确定一个树是否枚举完成(如果有的话请赐教),如果能的话就不用第二个函数去计算有多少种树。而且不知道给定两个序列最多有多少种树,所以也不能保证是否溢出结果是否正确。但实际运行的结果是有两个测试点段错误,不知道是不是数组越界还是栈溢出。
简便方法参考了其他人的方法。
自己的:
#include <iostream>#include <vector>#include <unordered_set>#include <map>using namespace std;struct node{int left,right,data;node(int d = 0,int l=-1,int r=-1):left(l),right(r){}};int id = 0,kase = 0,n;node tree[35];int pre[35],post[35];vector<int> ans;bool same(const unordered_set<int> & v1,const unordered_set<int> & v2){for(auto itr = v1.begin();itr != v1.end();++itr){if(v2.find(*itr) == v2.end())return false;}return true;}void inorder(int root){if(root != -1){inorder(tree[root].left);ans.push_back(tree[root].data);inorder(tree[root].right);}}int build_tree(int a1,int b1,int a2,int b2,bool & f,int level){int r= -1;f = false;if(b1 - a1 != 0){if(pre[a1] != post[b2-1]){f = false;return -1;}unordered_set<int> v1,v2;int r = id++,flag=0;tree[r].data = pre[a1];bool f1 = false,f2= false;tree[r].left = -1;f2=true;tree[r].right = build_tree(a1+1,b1,a2,b2-1,f2,level+1);if(f1&&f2){f = true;if(level == 0){inorder(0);}return r;}for(int i=0;i<b1-a1-1;i++){v1.insert(pre[a1+i+1]);v2.insert(post[a2+i]);if(same(v1,v2)==true){tree[r].left = build_tree(a1+1,a1+1+i+1,a2,a2+i+1,f1,level+1);tree[r].right = build_tree(a1+1+i+1,b1,a2+i+1,b2-1,f2,level+1);if(f1&&f2){f = true;if(level == 0){inorder(0);}return r;}}}tree[r].right = -1;f2=true;tree[r].left = build_tree(a1+1,b1,a2,b2-1,f1,level+1);if(f1&&f2){f = true;if(level == 0){inorder(0);}return r;}}else {f = true;}return r;}int just_one(int a1,int b1,int a2,int b2,bool & f,int level,int & cnt){int r= -1;f = false;int c1=0,c2=0;cnt = 0;if(b1 - a1 != 0){if(pre[a1] != post[b2-1]){f = false;return -1;}unordered_set<int> v1,v2;int r = id++,flag=0;tree[r].data = pre[a1];bool f1 = false,f2= false;tree[r].left = -1;f2=true; c1= 1;tree[r].right = just_one(a1+1,b1,a2,b2-1,f2,level+1,c2);if(f1&&f2){f = true;cnt += c1*c2;}for(int i=0;i<b1-a1-1;i++){v1.insert(pre[a1+i+1]);v2.insert(post[a2+i]);if(same(v1,v2)==true){tree[r].left = just_one(a1+1,a1+1+i+1,a2,a2+i+1,f1,level+1,c1);tree[r].right = just_one(a1+1+i+1,b1,a2+i+1,b2-1,f2,level+1,c2);if(f1&&f2){f = true;cnt += c1*c2;}}}tree[r].right = -1;f2=true;c2 = 1;tree[r].left = just_one(a1+1,b1,a2,b2-1,f1,level+1,c1);if(f1&&f2){f = true;cnt += c1*c2;}}else {f = true;cnt = 1;}return r;}void display(const vector<int> & vec){for(int i=0;i<vec.size();i++){if(i != 0)cout<<" ";cout<<vec[i];}cout<<endl;}int main(){cin>>n;for(int i=0;i<n;i++){cin>>pre[i];}for(int i=0;i<n;i++){cin>>post[i];}bool f;int r = build_tree(0,n,0,n,f,0);int cnt = 0;just_one(0,n,0,n,f,0,cnt);if(cnt == 1)cout<<"Yes\n";else cout<<"No\n";display(ans);return 0;}

参考了http://blog.csdn.net/liuchuo/article/details/52505179
#include <iostream>#include <vector>using namespace std;bool one = true;vector<int> res;int pre[35],post[35],n;//已经确定是合法的前后序遍历序列,所以不用判断是否合法。void handle(int a1,int b1,int a2,int b2){if(b1-a1>1){int idx = 0;for(int i=0;i<b1-a1-1;i++)if(pre[a1+1+i] == post[b2-2]){idx = i;break;}if(idx > 0){handle(a1+1,a1+1+idx,a2,a2+idx);res.push_back(pre[a1]);handle(a1+1+idx,b1,a2+idx,b2-1);}else{one = false;res.push_back(pre[a1]);handle(a1+1,b1,a2,b2-1);}}else if(b1-a1 == 1 )res.push_back(pre[a1]);}int main(){cin>>n;for(int i=0;i<n;i++)cin>>pre[i];for(int i=0;i<n;i++)cin>>post[i];handle(0,n,0,n);if(one == true)cout<<"Yes\n";else cout<<"No\n";for(int i=0;i<res.size();i++){if(i != 0)cout<<" ";cout<<res[i];}cout<<endl;return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 胃一阵阵疼然后拉肚子怎么办 橱子和墙壁不平怎么办 复印选项是英文不认识怎么办 防盗门锁与门框结合不好怎么办 仿瓷涂料墙壁脏了怎么办 油笔画到墙纸上怎么办 水笔画在墙纸上怎么办 屋里有股石灰味怎么办 厨房太阳对着晒怎么办 房子有太阳西斜怎么办 房子晒到太阳很热怎么办 房子被太阳热了怎么办 房间西晒窗帘不遮光怎么办 新建房屋一面墙体有裂缝怎么办 卫生间地砖缝隙出现渗水怎么办 西户窗户太晒怎么办 西晒的墙面很烫怎么办 儿童房颜色太粉了怎么办? 小孩在家里偷钱怎么办 脾气不好的猫该怎么办 二年孩子偷钱怎么办 孩子偷钱2000报警怎么办? 我儿子十岁老是偷钱怎么办 13孩子偷同学钱怎么办 孩子偷同学的钱怎么办 儿子十四岁了老偷钱怎么办 发现初中生的儿子偷钱怎么办 被亲戚怀疑儿子偷钱怎么办 房门选的太白了怎么办 大厅地砖颜色比墙砖浅怎么办 房屋外墙渗水物业不管怎么办 走丢了怎么办教学反思 托班教案迷路了怎么办 大班安全教案遇到小偷怎么办 小班孩子舞台表演找不到位置怎么办 懂你英语学完了怎么办 小班社会走丢了怎么办 帮小老鼠搬鸡蛋怎么办 小老鼠还能怎么办鸡蛋 中班教案走丢了怎么办 走丢了怎么办可后反思