PAT(A) 1119 Pre- and Post-order Traversals (30)(数据结构)
来源:互联网 发布:淘宝的冰裂杯有害吗 编辑:程序博客网 时间:2024/05/28 22:08
记录一个菜逼的成长。。
发现今年秋季的好简单。。亏了。
应该都写过知道中序和先序或者后序的一个然后重建树。
题目大意:
给你二叉树的先序和后序遍历序列。输出任意一种中序遍历序列(不唯一)。
知道先序和后序很容易知道根节点,然后从先序的根结点往后 和 后序序列的开头往后找按升序或降序排列后一样的序列,然后递归建树。
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cstdlib>#include <vector>#include <set>#include <map>#include <queue>#include <stack>#include <list>#include <deque>#include <cctype>#include <bitset>#include <cmath>#include <cassert>using namespace std;#define ALL(v) (v).begin(),(v).end()#define cl(a,b) memset(a,b,sizeof(a))#define bp __builtin_popcount#define pb push_back#define mp make_pair#define fin freopen("D://in.txt","r",stdin)#define fout freopen("D://out.txt","w",stdout)#define lson t<<1,l,mid#define rson t<<1|1,mid+1,r#define seglen (node[t].r-node[t].l+1)#define pi 3.1415926#define exp 2.718281828459#define lowbit(x) (x)&(-x)typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;typedef pair<LL,LL> PLL;typedef vector<PII> VPII;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;template <typename T>inline void read(T &x){ T ans=0; char last=' ',ch=getchar(); while(ch<'0' || ch>'9')last=ch,ch=getchar(); while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar(); if(last=='-')ans=-ans; x = ans;}const int maxn = 100 + 10;map<int,int>tree;int flag = 0;int pre[maxn],post[maxn];int vis[40];void build( int t,int l1,int r1,int l2,int r2 ){ if(pre[l1] == post[r2]){ tree[t] = pre[l1]; if(l1 == r1)return ; int p; for( int len = 0,i = l1 + 1; i <= r1; i++,len++ ){ cl(vis,0); for( int j = l1 + 1; j <= i; j++ ){ vis[pre[j]] = 1; } int f = 1; for( int j = l2; j <= l2 + len; j++ ){ vis[post[j]]--; if(vis[post[j]] < 0){ f = 0; break; } } if(f){ p = i; break; } } build(t<<1,l1+1,p,l2,p-l1+l2-1); build(t<<1|1,p+1,r1,p-l1+l2,r2-1); } else{ flag = 1; tree[t<<1] = pre[l1]; tree[t<<1|1] = pre[r1]; return ; }}int f = 1;void in_tra(int t){ if(tree[t]){ in_tra(t<<1); if(f)printf("%d",tree[t]),f = 0; else printf(" %d",tree[t]); in_tra(t<<1|1); }}int main(){ int n; while(~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]); } build(1,0,n-1,0,n-1); puts(flag ? "No" : "Yes" ); in_tra(1); puts(""); } return 0;}
0 0
- PAT(A) 1119 Pre- and Post-order Traversals (30)(数据结构)
- PAT(A) - 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
- 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 A1119 pre- and post-order traversals(30)
- PAT A1119. Pre- and Post-order Traversals (30)
- PAT 1119. Pre- and Post-order Traversals (30)
- PAT A1119. 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
- PAT (Advanced Level) Practise 1119Pre- and Post-order Traversals (30)
- android Px与Dp互转换
- HBase——使用Put迁移MySql数据到Hbase
- DOS之net命令的使用
- 【codeforces】C - Buggy Sorting
- 微信小程序开发实战——使用第三方库Immutable.js
- PAT(A) 1119 Pre- and Post-order Traversals (30)(数据结构)
- JS学习之ES6
- CSS中 !important 的使用
- Ubuntu14.04安装记录
- Android ImageView设置大小
- 2016年中国OTT盒子行业市场现状及发展前景分析
- 从零开始写一个Tomcat(叁)--请求解析
- PHP无限级分类实现(递归+非递归)
- 装饰者模式(Decorator Pattern) 实例