PAT A1020

标签(空格分隔): PAT


#include <cstdio>
#include <queue>
using namespace std; const int maxn = 100;
int pos[maxn], in[maxn];
int n; struct node {
int data;
node* lchild;
node* rchild;
}; node* create(int inL, int inR, int posL, int posR) {
if(posL > posR) return NULL;
node* root = new node;
root->data = pos[posR];
int k;
for(k = inL; k <= inR; k++) {
if(pos[posR] == in[k])
break;
}
int numberLeft = k - inL;
root->lchild = create(inL, inL + numberLeft - 1, posL, posL + numberLeft - 1);
root->rchild = create(inL + numberLeft + 1, inR, posL + numberLeft, posR - 1);
return root;
} int cnt = 0; //坑
void print(node* root) {
queue<node*> q;
q.push(root);
while(!q.empty()) {
node* now = q.front();
q.pop();
printf("%d", now->data);
cnt++;
if(cnt < n) printf(" ");
if(now->lchild != NULL) q.push(now->lchild);
if(now->rchild != NULL) q.push(now->rchild);
}
} int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &pos[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &in[i]);
}
node* ans = new node;
ans = create(1, n, 1, n);
print(ans);
return 0;
}

PAT A1020的更多相关文章

  1. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  2. PAT A1020 Tree Traversals(25)

    题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  3. [PAT] A1020 Tree Traversals

    [题目] distinct 不同的 postorder 后序的 inorder 中序的 sequence 顺序:次序:系列 traversal 遍历 题目大意:给出二叉树的后序遍历和中序遍历,求层次遍 ...

  4. PAT A1020——已知后序中序遍历求层序遍历

    1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...

  5. PAT题目AC汇总(待补全)

    题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...

  6. PAT_A1020#Tree Traversals

    Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ...

  7. PAT甲级——A1020 Tree Traversals

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  8. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  9. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

随机推荐

  1. npm版本安装问题

    问题一 描述 运行npm install之后,前端页面console控制台报错,invalid props. 排查 1. 排除了代码问题,完全一样的代码,其他人的运行无误. 2.猜想可能是版本号问题, ...

  2. python实现简单二分查找

    #!/usr/bin/pythondef binary_search(list, item): low = 0 high = len(list)-1 while low <= high: mid ...

  3. ExtJs写本地ArrayStore,ComboBox调用

    1.自定义本地ArrayStore var sCurStore = new Ext.data.ArrayStore({ //设备状态store fields: ["ckey", & ...

  4. C++学习建议

    C++学习建议 C++缺点之一,是相对许多语言复杂,而且难学难精.许多人说学习C语言只需一本K&R<C程序设计语言>即可,但C++书籍却是多不胜数.我是从C进入C++,皆是靠阅读自 ...

  5. MySQL 存储过程 if语句

    MySQL  存储过程 if语句 MySQL IF语句允许您根据表达式的某个条件或值结果来执行一组SQL语句. 要在MySQL中形成一个表达式,可以结合文字,变量,运算符,甚至函数来组合.表达式可以返 ...

  6. LOB

    一,LOB介绍 1,概念 LOB 是指用来存储大对象的数据类型,一般说LOB只是泛指,具体有BLOB,CLOB,NCLOB,BFILE.   根据你数据库的设置,一个LOB可以存储的最大大小从8TB到 ...

  7. ape 文件 转化为mp3 文件

    试了很多软件,最后才发觉 any-audio-converter最好用. 可以吧ape 按 cue切割好,然后转化成 MP3 官网可以免费下载: https://www.any-audio-conve ...

  8. SQL查询【根据生日计算】

    根据生日日期,获取当前年龄.年龄单位. Select Case when DateDiff(Year, BirthDate, GetDate()) > 0 then DateDiff(Year, ...

  9. 洛谷P1140 基因匹配 //DP真正意义上的一血

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了44种核苷酸,简记作A,C,G,TA,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务 ...

  10. js的一些常用方法

    1.判断是否为一个空对象 let a={}; console.log(Object.keys(arr).length==0);//true 2.从数组中取出重复的数据 var arr = [" ...