1119. Pre- and Post-order Traversals (30)
来源:互联网 发布:怎么测算淘宝单坑产出 编辑:程序博客网 时间:2024/05/22 13:19
有个坑,数字不是按1.2.3这样排序的
#include<iostream>#include<vector>#include<algorithm>using namespace std;struct node { int data; node *l, *r; node() { l = r = NULL; }};vector<node> all;vector<int> pre;vector<int> post;vector<int> dat;int N,flag=0;void change(int a, int b, int c, int d){ if (a >= b||c>=d) return; if (pre[a + 1] == post[d - 1]) { flag = 1; all[pre[a]].r = &all[pre[a + 1]]; change(a + 1, b, c, d - 1); } else { all[pre[a]].l = &all[pre[a + 1]]; all[pre[a]].r = &all[post[d - 1]]; int pos_pre = find(pre.begin()+1, pre.end(), post[d - 1])-pre.begin(); int pos_post= find(post.begin()+1, post.end(), pre[a+1])-post.begin(); change(a+1, pos_pre - 1, c, pos_post); change(pos_pre, b, pos_post+1, d-1); }}int cnt = 0;void InOrder(node *root){ if (root == NULL) return; InOrder(root->l); if (!cnt++) printf("%d", root->data); else printf(" %d", root->data); InOrder(root->r);}int find_index(int x){ for (int t = 1;t <= N;t++) if (x == all[t].data) return t;}int main(){ cin >> N; all.resize(N+1); pre.resize(N+1); post.resize(N+1); dat.resize(N + 1); for (int t = 1;t <= N;t++) { cin >> all[t].data; pre[t]=t; } for (int t = 1;t <= N;t++) { int tem; cin >> tem; post[t] = find_index(tem); } change(1, N, 1, N); if (flag == 1) cout << "No" << endl; else cout << "Yes" << endl; node *root = &all[pre[1]]; InOrder(root); cout << endl;}
0 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
- 1053. Path of Equal Weight
- ActiveMQ
- SuppressLint解决办法
- [SDOI2010]大陆争霸
- 项目3
- 1119. Pre- and Post-order Traversals (30)
- selenium中的junit单元测试框架使用_03
- 用phpcms如何将静态页面制作成企业网站(中)
- C++第一次实验项目1
- ubuntu16.04 安装shadowsocks,番羽土啬
- 232. Implement Queue using Stacks
- Android的HAL
- 漫谈android系统(5)点亮LED
- 2017.2.27自测