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 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 <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;}
- 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
- 【PAT】1119. Pre- and Post-order Traversals
- mysql总结
- myeclispe快捷键
- 微信分享 微信对图片文字大小的限制 代码如下
- 事件分发机制
- RxBus的小试牛刀
- 1119. Pre- and Post-order Traversals (30)
- Android 自定义View (一)
- bzoj 1047 理想的正方形
- java基础面试题1-----String,StringBuffer,StringBuilder的区别
- Xcode 8制作动态及静态Framework
- 微信公众号网页上点击放大图片浏览,解决方案
- angularJS之项目知识
- maven 打包时,总是少了一个包,后来发现是pom 文件的scope 的类型导致
- react native 中DataPickerAndroid的用法和一些注意注意点