PAT (Advanced Level) Practice 1099 Build A Binary Search Tree (30 分)
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 (≤100) 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 −1 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
通信通法:先sort排好序,然后中序插入,之后队列遍历输出,完美AC~
#include<bits/stdc++.h>
#define ll long long
#define rg register ll
using namespace std;
struct node
{
ll l,r,val;
node()
{
l=r=-1;
}
}p[105];
ll a[105],cnt;
inline void init(ll x)
{
if(x!=-1)
{
init(p[x].l);
p[x].val=a[cnt++];
init(p[x].r);
}
else return;
}
queue<ll>q;
vector<ll>ans;
int main()
{
ll n;
cin>>n;
for(rg i=0;i<n;i++)
{
cin>>p[i].l>>p[i].r;
}
for(rg i=0;i<n;i++)cin>>a[i];
sort(a,a+n);
init(0);
q.push(0);
while(!q.empty())
{
ans.push_back(p[q.front()].val);
if(p[q.front()].l!=-1)q.push(p[q.front()].l);
if(p[q.front()].r!=-1)q.push(p[q.front()].r);
q.pop();
}
for(rg i=0;i<ans.size();i++)
{
i==ans.size()-1?cout<<ans[i]<<endl:cout<<ans[i]<<" ";
}
while(1)getchar();
return 0;
}
- MySQL 5.0和PostgreSQL 8.1
- Python入门讲解
- DDM: 一个简洁的前端领域模型库
- 前后端分离之领域模型的思考
- 类选择符和ID选择符
- 仿网易新闻页面效果
- VS.Net 2005 Design-Time Integration
- XML Serializable Generic Dictionary
- MATLAB简易验证码识别程序介绍
- android之listview缓存图片(缓存优化)
- 使用ASP.NET实现Model View Presenter(MVP)
- android性能优化1
- 百度地图之收索视野内的建筑物
- Line Counter - Writing a Visual Studio 2005 Add-In
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- R语言ARMA-EGARCH模型、集成预测算法对SPX实际波动率进行预测
- nginx快速入门
- R语言中进行期权定价的Heston模型
- 使用R语言随机波动模型SV处理时间序列中的随机波动率
- 20个ES6面试高频问题
- i++和++i傻傻分不清楚?这里给你最清楚的解答
- android APT 使用
- Flutter异步编程async与await的基本使用
- 教大家一个万能PPT图片排版技巧,太赞了!
- 重复读取 HttpServletRequest 中 InputStream 的方法
- 测试面试题集锦(三)| 计算机网络和数据库篇(附答案)
- 关于Scikit-Learn你(也许)不知道的10件事
- 技术天地 | CSS-in-JS:一个充满争议的技术方案
- 安全研究 | 通过域名劫持实现Azure DevOps账户劫持
- 一款针对DLL劫持的恶意DLL生成器