PAT (Advanced Level) 1064. Complete Binary Search Tree (30)
因为是要构造完全二叉树,所以树的形状已经确定了。
因此只要递归确定每个节点是多少即可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<stack>
#include<map>
#include<algorithm>
using namespace std; int a[+],ans[+];
int f[+],sz[+];
int n; void t(int id)
{
if(*id>n&&*id+>n)
{
f[id]=; sz[id]=; return;
} if(*id<=n) t(*id);
if(*id+<=n) t(*id+); f[id]=sz[*id];
sz[id]=sz[*id]+sz[*id+]+;
} void dfs(int id,int L,int R)
{
ans[id]=a[L+f[id]];
if(*id<=n) dfs(*id,L,L+f[id]-);
if(*id+<=n) dfs(*id+,L+f[id]+,R);
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a++n); memset(f,,sizeof f);
memset(sz,,sizeof sz);
t(); dfs(,,n); for(int i=;i<=n;i++)
{
printf("%d",ans[i]);
printf("%s",(i<n)?" ":"\n");
} return ;
}
PAT (Advanced Level) 1064. Complete Binary Search Tree (30)的更多相关文章
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
随机推荐
- MFC中实现定时执行与提醒功能(自编代码)
具体实现代码如下:添加一个计时器:SetTimer(1,1000,NULL); 下面仅列举核心代码,详细步聚不作说明,效果如下所示: void CShowTimer::OnTimer(UINT_PTR ...
- js 插入图片切换,innerHTML
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- js 条件判断放大字体
<html> <head> <meta charset="utf-8" /> <title></title> <s ...
- CSS3秘笈:第七章
第七章 margin.padding和border 1.盒模型:四个属性: (1)padding:内容与其边框线之间的空间. (2)border:盒子周围的直线 (3)background-colo ...
- LeetCode OJ 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- H5之重力感应篇
手机的重力感应支持里,有两个主要的事件: 1. OrientationChange (在屏幕发生翻转的时候触发) 2. DeviceOrientation+DeviceMotion(重力感应与陀螺仪) ...
- mongo细节
mongo创建表db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean>, size: < ...
- Redis配置文件 翻译 V3.2版本
# Redis配置文件例子. # # 注意:为了能读取到配置文件,Redis服务必须以配置文件的路径作为第一个参数启动 # ./redis-server /path/to/redis.conf # 关 ...
- jsonarray和jsonobject
JSONArray ja = new JSONArray(); ja.put("11"); ja.put("22"); ja.put("33" ...
- linux的学习系列 3---目录
目录也是一个文件,它的唯一功能是用来保存文件及其相关信息.所有的文件,包括普通文件.设备文件和目录文件,都会被保存到目录中. 主目录 登录后,你所在的位置就是你的主目录(或登录目录),接下来你主要是在 ...