PAT A1020
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的更多相关文章
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
		Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ... 
- PAT A1020 Tree Traversals(25)
		题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ... 
- [PAT] A1020 Tree Traversals
		[题目] distinct 不同的 postorder 后序的 inorder 中序的 sequence 顺序:次序:系列 traversal 遍历 题目大意:给出二叉树的后序遍历和中序遍历,求层次遍 ... 
- PAT A1020——已知后序中序遍历求层序遍历
		1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ... 
- PAT题目AC汇总(待补全)
		题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ... 
- PAT_A1020#Tree Traversals
		Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ... 
- PAT甲级——A1020 Tree Traversals
		Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ... 
- PAT甲级题解分类byZlc
		专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ... 
- 《转载》PAT 习题
		博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ... 
随机推荐
- 前端学习之CSS
			CSS介绍 CSS(Cascading Style Sheet, 层叠样式表)定义如何显示HTML元素, 给HTML设置样式, 让它更加美观. 当浏览器读到一个样式表, 它就会按照这个样式表来对文档进 ... 
- radio(单选框)反复选中与取消选中
			做个记录,以便需要拿取 <script type="text/javascript"> $(function(){ 第一种 $('input:radio').click ... 
- 基于redis的分布式锁(转)
			基于redis的分布式锁 1 介绍 这篇博文讲介绍如何一步步构建一个基于Redis的分布式锁.会从最原始的版本开始,然后根据问题进行调整,最后完成一个较为合理的分布式锁. 本篇文章会将分布式锁的实现分 ... 
- 图解HTTP学习笔记
			前言: 一直觉得自己在HTTP基础方面都是处于知其然,不知其所以然的样子.最近利用空闲时间拜读了一下图解HTTP,写篇博客记录一下读书笔记. TCP三次握手: ① 发送端首先发送一个带SYN标志的数据 ... 
- 雷林鹏分享:Laravel 安装
			前面我们介绍我了 composer安装,这里我们接着来介绍 Laravel框架的安装. 这里我们安装的是laravel 4 项目下载地址:https://github.com/laravel/lara ... 
- google搜索引擎爬虫爬网站原理
			google搜索引擎爬虫爬网站原理 一.总结 一句话总结:从几个大站开始,然后开始爬,根据页面中的link,不断爬 从几个大站开始,然后开始爬,根据页面中的link,不断加深爬 1.搜索引擎和数据库检 ... 
- python基础知识点(unittest)
			目录: unittest 单元测试框架 1.写用例: Testcase 2.执行:TestSuite 类 TestLoader 类 3.比对结果(期望值/实际值):断言函数 4.结果:TestText ... 
- 1.1 Django起步
			1.1 Django起步 1.1.1. Django简介 Django开发框架(简称Django)诞生的时间是2003年的金秋时节,美国有两位程序员Adrian Holovaty和Simon ... 
- 【转】TCP、UDP、RTP(RTCP)区别
			转自:https://www.cnblogs.com/imystr/p/4026639.html OSI七层模型OSI 中的层 功能 ... 
- js浮点数运算精度问题
			https://www.cnblogs.com/yadongliang/p/9067333.html 
