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 - vue 全面面试题)
记录近期换工作时遇到的面试题和面试题答案 css 部分 盒模型 问题:说一下 css 的盒模型 盒模型分为标准模型和怪异盒模型(IE 盒模型) 标准盒模型:盒模型的宽高只是内容(content)的宽高 ...
- linux 系统下安装多个php版本
思路: 下载不同的php源码包,解压后安装在不同的目录下,修改php-fpm监听的端口号 php安装配置参数: ./configure --prefix=/usr/local/php71 --exec ...
- Jenkins去GitLab拉取Java代码自动打包
jenkins的部署 一.部署git 1)先检查系统是否已经自带了git,如果有,就卸载 $ rpm -qa | grep git && rpm -e git --nodeps 2)开 ...
- centos7 基础命令
一: linux基础 (1) 查看服务器的IP信息 ip add showifconfig (2) 操作网卡命令(重启网络和启用网卡) systemctl restart networksystemc ...
- Java面试题之Java基础
1. JDK 和 JRE 有什么区别? JRE(JavaRuntimeEnvironment,Java运行环境),也就是Java平台.所有的Java 程序都要在JRE下才能运行.普通用户只需要运行已开 ...
- MIUI6系统如何启用root权限的教程
MIUI6系统有没有办法启用了root权限?大家都清楚,Android机器有root权限,如果手机启用了root相关权限,就能够实现更好的功能,举例子,大家单位的营销部门同事,使用某些营销软件都需要在 ...
- oralce问题
死锁,如果较多使用存储过程杀死 create or replace procedure killer is v_obj varchar2(200); v_sql varchar2(500) ...
- crop image 需要的基础知识
refer : https://www.youtube.com/watch?v=R7dObDtw1aA https://www.shuxuele.com/algebra/trig-finding-an ...
- windows下面Nginx日志切割
Nginx本身并不支持日志切割,那么就会造成日志非常的大,为了解决这个问题我们用到了windows的计划任务和dos命令.具体思路: 1.写一个dos文件,通过windows的计划任务定时执行(每天执 ...
- (7)udp-socket
# upd 协议:(1)#client 端口:import socketsk = socket.socket(type=socket.SOCK_DGRAM)message = "我来了大哥& ...