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. Linux输入输出重定向练习

    1.date >> 123 date > 123 2.abc 2>123 abc 2>>123 abc 2>/dev/null  标准输出重定向到回收站 3. ...

  2. CF388C&&2018EC Final D题——博弈&&水题

    一下两个题目都是按堆取石子,轮流取,每个人都贪心的取即可,感觉都不像博弈. CF388C 有n排石子,每排有若干堆.Ciel可以选择一排,拿走这一排的第一堆石子.Jiro可以选择一排,拿走这一排的最后 ...

  3. A1136 | 字符串处理、大整数运算

    题目链接: https://www.patest.cn/contests/pat-a-practise/1136 今天是12月17号.最近这几天都有点不在状态.已经整整一周没有练算法了,自从12.3考 ...

  4. A_G_C_007

    AGC007 A Shik and Stone 我是沙比这都能蛙一发 https://agc007.contest.atcoder.jp/submissions/7946110 B Construct ...

  5. P3386 【模板】二分图匹配(匈牙利算法)

    题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正整数u,v,表示u,v有一条连边 ...

  6. UE4的联网系统研究

    1. 物体复制 具体细节可参考官网内容:http://api.unrealengine.com/CHN/Gameplay/Networking/index.html 这里只挑部分点来展开. 首先,分为 ...

  7. linux 排查cpu负载过高异常

    步骤一.找到最耗CPU的进程 工具:top 方法: 执行top -c ,显示进程运行信息列表 键入P (大写p),进程按照CPU使用率排序 图示: 如上图,最耗CPU的进程PID为10765 步骤二: ...

  8. leetcode: 最长上升子序列

    题目描述: 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18]输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 ...

  9. el-select获取选中的value和label

    selectGetFn(val) { if (this.multipleFlag) { for (let i = 0; i < GUIDArr.length; i++) { let obj =  ...

  10. wordpress 本地环境安装

    1. 下载xmapp 2. 安装mysql 3. 启动xmapp的数据库与Apache,通常无法启动原因. mac的先关闭自己的数据库,系统偏好设置-MySQL Apache的端口默认是80,一般会被 ...