https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648

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 Nlines 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; int N; struct Node{
int data;
int l, r;
}node[110]; vector<int> v;
int ans = 0; void inorder(int root) {
if(root == -1) return ; inorder(node[root].l);
node[root].data = v[ans ++];
inorder(node[root].r);
} void levelorder(int root) {
queue<int> q;
if(root != -1) q.push(root);
bool isfirst = true; while(!q.empty()) {
int t = q.front();
q.pop(); if(isfirst) {
printf("%d", node[t].data);
isfirst = false;
}
else printf(" %d", node[t].data); if(node[t].l != -1) q.push(node[t].l);
if(node[t].r != -1) q.push(node[t].r);
}
} int main() {
scanf("%d", &N);
for(int i =0; i < N; i ++)
scanf("%d%d", &node[i].l, &node[i].r);
v.resize(N);
for(int i = 0; i < N; i ++)
scanf("%d", &v[i]);
sort(v.begin(), v.end());
inorder(0);
levelorder(0);
return 0;
}

  这这这 应该是学会建树了叭 unbelieveable 有一点点鸡冻 

PAT 甲级 1099 Build A Binary Search Tree的更多相关文章

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

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

  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甲级——A1099 Build A Binary Search Tree

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

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

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

  7. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

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

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

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

随机推荐

  1. LFS搭建第一天补充

    1.选择lfs的iso 2.启动虚拟机,进入以下界面直接按回车键 3. 选择下面的时间 4.直接默认本地时间 5.语言格式选择UTF-8 6.默认 7.直接回车,进入系统 8.对hda盘进行分区,我分 ...

  2. 欧几里得算法/欧几里得扩展算法-python

    说在开头. 出于对欧几里得的尊重,先简单介(cou)绍(ge)一(zi)下(shu).. 欧几里得,古希腊人,数学家.他活跃于托勒密一世时期的亚历山大里亚,被称为“几何之父”. 他最著名的著作< ...

  3. SAP BDC 交货增强无法进入

    最近碰到一个问题,使用BDC MODE 'N' UPDATE 'S' 无法进入增强: FV50XF0B_DELIVERY_PUBLISH DELIVERY_PUBLISH_AFTER_SAVE 前台操 ...

  4. go VS NET 字符串操作能力

    今天拿golang 与 NET4.0 做了在字符串方面的性能比较,看看谁牛! 一.读取txt文本文件 GO的代码: readbuf, _ := ioutil.ReadFile(userFile) st ...

  5. 【Hadoop】Hadoop 中 RPC框架原理、代码示例

    0.内容 1.hadoop中的RPC框架封装思想 2.Hadoop RPC 实现方法 3.服务调用动态转发和负载均衡的实现思考 4.协议代码: package com.ares.hadoop.rpc; ...

  6. # 20155224 2016-2017-2 《Java程序设计》第10周学习总结

    20155224 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 密码学: 主要是研究保密通信和信息保密的学科, 包括信息保密传输和信息加密存储等. 密码学 ...

  7. 20155310《Java程序设计》实验五(网络编程与安全)实验报告

    20155310<Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 •任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上 ...

  8. nginx重启无法找到PId的解决办法

    nginx停止的时候,出现了如下错误 nginx: [error] open() "/export/servers/nginx/logs/nginx.pid" failed (2: ...

  9. Oracle下如何用rman备份到特定的sequence

    本文为摘抄,目的为方便日后阅读: http://docs.oracle.com/cd/B12037_01/server.101/b10734/rcmbackp.htm To determine the ...

  10. [2016北京集训试题8]连在一起的幻想乡[dp+无向图计数]

    Description Solution 本博客参考yww大佬的博客,为了加深理解我就自己再写一遍啦. 以下的“无向图”均无重边无自环. 定义f0[n]为n个点构成的无向图个数,f1[n]为n个点构成 ...