1099 Build A Binary Search Tree (30 分)
 

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

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format left_index right_index, provided that the nodes are numbered from 0 to N−1, and 0 is always the root. If one child is missing, then − will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.

Output Specification:

For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42

Sample Output:

58 25 82 11 38 67 45 73 42
//根据二叉树的结构和中序遍历来写出层序遍历
#include <bits/stdc++.h>
using namespace std;
#define N 120
#define P pair<int,int>
struct Tree{
int val,l,r;
Tree(){
}
Tree(int val,int l,int r):val(val),l(l),r(r){
}
}tr[N];
int a[N],b[N],n;
//不需要建树,题目输入的就是了
int cnt2=;
void search(int rt){//中序
if(rt==-) return ;
search(tr[rt].l);
tr[rt].val=a[cnt2++];
search(tr[rt].r);
}
int cnt = ;
void dfs(int rt){//层序遍历
queue<P>q;
q.push(P(rt,tr[rt].val));
int flag =;
while(!q.empty()){
P p =q.front();q.pop();
int x=p.first,y=p.second;
//输出格式
if(flag){
printf("%d",y);
flag =;
}
else{
printf(" %d",y);
}
int num1 = tr[x].l;
int num2= tr[x].r;
if(num1!=-){
q.push(P(num1,tr[num1].val));
}
if(num2!=-){
q.push(P(num2,tr[num2].val));
}
}
}
int main()
{
scanf("%d",&n);
for(int i =;i<n;i++){
scanf("%d%d",&tr[i].l,&tr[i].r);
}
for(int i =;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
search();
dfs();
return ;
}

PAt 1099的更多相关文章

  1. PAT 1099 Build A Binary Search Tree[BST性质]

    1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  2. PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)

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

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

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

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

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

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

  6. PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

    预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...

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

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

  8. PAT 甲级 1099 Build A Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...

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

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

随机推荐

  1. Buy Fruits-(构造)

    https://ac.nowcoder.com/acm/contest/847/C 在blueland上有 n n个水果店,它们的编号依次为 0,1,2...n−1 0,1,2...n−1.奇妙的是, ...

  2. RabbitMQ六种队列模式-发布订阅模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅 [本文]RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  3. 一篇文章把你带入到JavaScript中的闭包与高级函数

    在JavaScript中,函数是一等公民.JavaScript是一门面向对象的编程语言,但是同时也有很多函数式编程的特性,如Lambda表达式,闭包,高阶函数等,函数式编程时一种编程范式. funct ...

  4. OpenFOAM——在钝板上分离的层流

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL063: Separated Laminar Flow Over a Blunt ...

  5. KAFKA && zookeeper 集群安装

    服务器:#vim /etc/hosts10.16.166.90 sh-xxx-xxx-xxx-online-0110.16.168.220 sh-xx-xxx-xxx-online-0210.16.1 ...

  6. jvm (一)jvm结构 & 类加载 & 双亲委托模型

    参考文档: jvm内幕-java虚拟机详解:http://www.importnew.com/17770.html 常量池:https://www.jianshu.com/p/c7f47de2ee80 ...

  7. 【Beta】Scrum meeting 9

    目录 写在前面 进度情况 任务进度表 Beta-1阶段燃尽图 遇到的困难 照片 commit记录截图 文档集合仓库 后端代码仓库 技术博客 写在前面 例会时间:5.13 22:30-22:45 例会地 ...

  8. [技术博客] rails控制台调试路由

    目录 rails console 获得路由 app.xxx_path 发送请求 获得响应 作者:庄廓然 rails console 在项目目录下执行rails console test 可以进入测试模 ...

  9. bind named.conf 的理解

    [root@46 /]#yum -y install bind bind-chroot bind-libs bind-utils caching-nameserver目录说明/var/named/ch ...

  10. 多线程高效合作之master-warker模式

    对于高并发的任务,有些任务是相互独立的,任务与任务之间没有依赖关系,因此可以采用 master - worker 模式. master 用于接受任务和分发任务给 worker,并将 worker 返回 ...