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. 前端学习之HTML

    HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...

  2. Vue-admin工作整理(十一):Vuex-动态注册模块

    概述: 动态注册模块分为两种,一种是在根state下注册一个模块,一种是在模块下再自动注册一个模块 第一种:根state下动态注册模块: 思路:通过store来实现,this.$store来获取当前的 ...

  3. dp入门之01背包问题

    ...通过暴力手推得到的一点点感觉 动态规划是相对于贪心算法的一种取得最优解的算法,通过对每一步的取舍判断从 0 推到所拥有的第 n 件物品,每次判断可以列写出状态转移方程,通过记忆化相对暴力地取得最 ...

  4. jenkin服务关闭和重启

    1.关闭Jenkins http://localhost:8080/exit 2.重启Jenkies http://localhost:8080/restart 3.重新加载配置信息 http://l ...

  5. java 获取微信公众号code为空

    失败的原因是没将回调方法encode转换 /** * URL编码(utf-8) * * @param source * @return */ public static String urlEncod ...

  6. springboot 使用Filter

    1. 创建 Filter 类,实现 Filter 接口 import javax.servlet.*; import javax.servlet.annotation.WebFilter; impor ...

  7. vue2 商城首页轮播图切换

    home.vue <template> <div class="home"> <HomeBanner></HomeBanner> & ...

  8. VS 2017 创建类注释模板

    在VS 2017/2019等 同样打开下方路径 C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\I ...

  9. Wincc报表+Listview使用

    listview在Wincc中可以作为显示的控件,对于列表表头的定义如下所示: list的命名,点击属性,在对象名称中对其定义: 有了listview的定义,就可以使用VBS对其表头的定义.具体代码如 ...

  10. springboot使用fastjson中文乱码解决方法 【转载】

    以前使用fastjson替换jackson时,没有直接在页面打印过json,都是js使用没有出现乱码,偶然 打印出来出现了中文乱码 之前使用的配置方式 @Configuration public cl ...