PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点。
然后确定值得时候递归下去就可以了。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; struct Node
{
int val;
int left;
int right;
int numL,numR;
}s[];
int n,a[];
vector<int>ans; void dfs(int x)
{
if(s[x].left!=-)
{
dfs(s[x].left);
s[x].numL=s[s[x].left].numL+s[s[x].left].numR+;
} if(s[x].right!=-)
{
dfs(s[x].right);
s[x].numR=s[s[x].right].numL+s[s[x].right].numR+;
}
} void Find(int x,int L,int R)
{
s[x].val=a[L+s[x].numL];
if(s[x].left!=-) Find(s[x].left,L,L+s[x].numL-);
if(s[x].right!=-) Find(s[x].right,L+s[x].numL+,R);
} void bfs()
{
queue<int>q;
q.push();
while(!q.empty())
{
int h=q.front(); q.pop();
ans.push_back(s[h].val);
if(s[h].left!=-) q.push(s[h].left);
if(s[h].right!=-) q.push(s[h].right);
} }
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++) {
scanf("%d%d",&s[i].left,&s[i].right);
s[i].numL=s[i].numR=;
}
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a++n);
dfs();
Find(,,n);
bfs();
for(int i=;i<ans.size();i++)
{
printf("%d",ans[i]);
if(i<ans.size()-) printf(" ");
else printf("\n");
}
return ;
}
PAT (Advanced Level) 1099. Build A Binary Search Tree (30)的更多相关文章
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...
- 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 ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- pat1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
随机推荐
- redis的适应场景
redis应用场景: 1.对数据高并发读写 2.对海量数据的高效存储和访问 3.对数据的高可扩展性和高可用性 做分布式扩展很简单,因为没有固定的表结构 redis介绍: redis是一个key-val ...
- 数据库 mysql 语句
LAMP: Linux系统 A阿帕奇服务器 Mysql数据库 Php语言 mysql:常用代码 create table CeShi1( Uid varchar(50) primary key, Pw ...
- 【Machine Learning in Action --3】决策树ID3算法
1.简单概念描述 决策树的类型有很多,有CART.ID3和C4.5等,其中CART是基于基尼不纯度(Gini)的,这里不做详解,而ID3和C4.5都是基于信息熵的,它们两个得到的结果都是一样的,本次定 ...
- 网页 JavaScript
今天,我主要学习了JavaScript的部分内容. JavaScript在html中的位置主要有三个地方:head里面.body里面.</html>之后,我们一般写在</html&g ...
- 1.Perl 多线程:Threads
详情可查看: perldoc threads 调用线程的方法: $thr = threads->create(FUNCTION, ARGS) #This will create a new th ...
- HDU 3499 Flight spfa+dp
Flight Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Subm ...
- HDU 5775 Bubble Sort
对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...
- git 免密码提交代码
Linux或者Mac下方法: 创建文件,进入文件,输入内容: cd ~ touch .git-credentials vim .git-credentials https://{username}:{ ...
- hdu_1254_推箱子(双BFS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 题解:以箱子为主体,第一层BFS,然后用第二层BFS来判断人是否可以到达,这里细节比较多,要注意 ...
- LeetCode OJ 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...