预处理每个节点左子树有多少个点。

然后确定值得时候递归下去就可以了。

#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)的更多相关文章

  1. 【PAT甲级】1099 Build A Binary Search Tree (30 分)

    题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...

  2. pat 甲级 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  3. 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 ...

  4. 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 ...

  5. 1099. Build A Binary Search Tree (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  6. PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

    题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...

  7. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  8. 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 ...

  9. pat1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

随机推荐

  1. Python 学习笔记4

    我是一个艺术家. 今天继续学习python啊.争取看到python流程控制.

  2. KVM 命令行启动第一台虚拟机

    KVM创建第一台虚拟机 1 创建一个镜像 [root@kvm ~]# qemu-img create -f raw /opt/CentOS6.-x86_64.raw 5G Formatting [ro ...

  3. C Runtime Library来历, API, MFC, ATL关系

    首先说明,我google了半天,想找到英文的关于这个资料,但是实在找不到,只好转载国人的讨论. CRT原先是指Microsoft开发的C Runtime Library,用于操作系统的开发及运行.后来 ...

  4. PHP通过url下载远程图片到本地

    function GrabImage($url,$filename) { if($url==""):return false;endif; ob_start(); readfile ...

  5. log4j配置详解[http://www.iteye.com/topic/378077]

    log4j是一个非常强大的log记录软件,下面我们就来看看在项目中如何使log4j. 首先当然是得到log4j的jar档,推荐使用1.2.X版,下载地址: http://logging.apache. ...

  6. C相关的图书(链接不可用)

    Visual C++视频技术方案宝典.pdf: http://www.t00y.com/file/17628500 Windows 图形编程.pdf: http://www.t00y.com/file ...

  7. 1213 How Many Tables 简单的并查集问题

    my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std; ...

  8. 素数槽csuoj

    超时代码: #include <iostream> using namespace std;//写一个函数判断是否是素数bool isPrime(int num){int i=2;//co ...

  9. LeetCode OJ 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  10. shell:crontab

    crontab */1 * * * * (cd /home/q/system/project; /usr/bin/lockf -t 0 /tmp/discuz_bbs_audit.lock /usr/ ...