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. [IoC容器Unity]第三回:依赖注入

    1.引言 上节介绍了,Unity的Lifetime Managers生命周期,Unity具体实现依赖注入包含构造函数注入.属性注入.方法注入,所谓注入相当赋值,下面一个一个来介绍. 2.构造函数注入 ...

  2. Layui使用心得(1)---- 数据表格

    前端的框架我了解的不是太多,现在在用Layui的框架开发.之后准备转向Vue和React 这一系列博客主要讲一些我使用的Layui的常用组件的心得,官方的网站讲解已经很详细了,这里我只是结合我的实际使 ...

  3. 页面对象(Page Object)模式

    内容转载自 https://www.cnblogs.com/yytesting/p/6973474.html 页面对象(Page Object)模式是目前自动化测试领域普遍使用的设计模式之一,此模式可 ...

  4. 树中的路径和 Sum of Distances in Tree

    2019-03-28 15:25:43 问题描述: 问题求解: 写过的最好的Hard题之一. 初看本题,很经典的路径和嘛,dfs一遍肯定可以得到某个节点到其他所有节点的距离和.这种算法的时间复杂度是O ...

  5. English trip EM1 - PE2 My My name is... Teacher:Lamb Key: introduce myself

    课上内容(Lesson) Lamb let us does introduce myself. Make a "hangman" game at warm-up . How to ...

  6. SQL添加事务处理

    --modi by lmt declare @errorSum int --记录错误数 begin Create table #CheckreqAccState(CheckReqID varchar( ...

  7. nginx配置二级目录,反向代理不同ip+端口

    场景描述: 通过二级目录(虚拟目录,应用程序)的方式访问同一ip+端口的不同应用,例如location是用户使用页面,location/admin/是管理页面,location部署在192.168.1 ...

  8. subline 建立服务器

    http://blog.csdn.net/csdn_chenli/article/details/53173953

  9. python中OS模块方法

    常见函数: os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.ge ...

  10. 命令“copy /V 已退出,代码为 1

    这个错误出现在vs生成事件里的命令行里. 第一种:简单粗暴直接清空命令行 第二种:通过输出的打印结果来分析: 比如我的出现这个原因一般有两种情况: 第一种:是可能我引用的共享盘Z盘无法连接: 第二种: ...