pat 1119. Pre- and Post-order Traversals (30)
来源:互联网 发布:软件项目开发管理制度 编辑:程序博客网 时间:2024/06/04 18:51
https://www.patest.cn/contests/pat-a-practise/1119
#include <cstdio>typedef struct node {int v;struct node * left, *right;node(int x) : left(NULL), right(NULL){v = x;}}Node;bool build(Node * &root, int* pre, int* post,int len) {if (len == 0) return true;root = new Node(*pre);pre++; post[len - 1] = 0;//find the sub treeint count = 0;bool luniq = false, runiq = false;while (*pre) {int i=0, newPre[31], newPost[31];while (pre[0] != post[i] ) {newPre[i] = pre[i];newPost[i] = post[i];i++;}//Add the last element.newPre[i] = pre[i];newPost[i] = post[i];newPre[i + 1] = 0;newPost[i + 1] = 0;pre = pre + i + 1;post = post + i + 1;count++;if (root->left == NULL) luniq = build(root->left, newPre, newPost, i+1);else runiq = build(root->right, newPre, newPost, i+1);}return count==0 ||(luniq && runiq);}bool first = true;void inOrder(Node * root) {if (root == NULL) return;inOrder(root->left);if (first) {printf("%d", root->v);first = false;}else {printf(" %d", root->v);}inOrder(root->right);}int main(){int n;int pre[31], post[31];scanf("%d", &n);for (int i = 0; i < n; i++){scanf("%d", &pre[i]);}for (int i = 0; i < n; i++){scanf("%d", &post[i]);}pre[n] = post[n] = 0;Node *root = NULL;bool uniq = build(root, pre, post, n);if (uniq) printf("Yes\n");else printf("No\n");inOrder(root);printf("\n"); return 0;}
另附参考了:http://www.liuchuo.net/archives/2484
#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;}
0 0
- PAT--1119. Pre- and Post-order Traversals
- (PAT)1119. Pre- and Post-order Traversals
- 【PAT】1119. Pre- and Post-order Traversals
- PAT 1119. Pre- and Post-order Traversals (30)
- 【PAT】1119. Pre- and Post-order Traversals (30)
- PAT 1119. Pre- and Post-order Traversals (30)
- pat 1119. Pre- and Post-order Traversals (30)
- PAT 1119. Pre- and Post-order Traversals (30)
- 【PAT】【Advanced Level】1119. Pre- and Post-order Traversals (30)
- PAT 1119. Pre- and Post-order Traversals (30) 用前序、后序求中序
- PAT 甲级 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30) PAT
- 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)
- 俗解HTTPS
- python中的list以及list与array相互转换
- poj 1416 Shredding Company(dfs)
- 使用C++封装库在MDK上建STM32开发工程
- sql 数据库表如何添加字段
- pat 1119. Pre- and Post-order Traversals (30)
- 简单的查询
- C--最短路(Bellman-Ford或者SPFA)
- java反射机制的作用与优点
- 自动装箱拆箱详解
- 41、Linux初步(学习到的linux命令)
- 编程学习序章
- Oracle数据库大并发下保证订单流水号的唯一性
- 算法导论——二维平面上的最邻近点对