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;
}
- iBatis.Net(6):Data Map(深入)
- iBatis.Net(5):Data Map(了解)
- ModernPHP读书笔记(三)——PHP的良好实践
- PHP开发过程的那些坑(一) ——对象拷贝
- PHP开发过程的那些坑(二) ——PHP empty函数
- Thinking in SQL系列之数据挖掘Apriori关联分析再现啤酒尿布神话
- PHP开发过程的那些坑(三) ——PHParray_shift函数
- CSS3弹性盒布局
- iBatis.Net(4):DataMapper API
- PHP开发过程的那些坑(四) ——PDO bindParam函数
- iBatis.Net(3):创建SqlMapper实例
- PHP开发过程的那些坑(五) ——PHP的empty()
- iBatis.Net(2):基本概念与配置
- ASP.NET Web API中的依赖注入什么是依赖注入ASP.NET Web API依赖解析器使用Unity解析依赖配置依赖解析
- 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 数组属性和方法
- android studio 使用Mocklocation虚拟定位
- Android自定义gridView仿头条频道拖动管理功能
- Android实现选项菜单子菜单
- Android scrollview如何监听滑动状态
- android 自定义圆角button效果的实例代码(自定义view Demo)
- Android开发简易音乐播放器
- Android 自定义ListView实现QQ空间界面(说说内包含图片、视频、点赞、评论、转发功能)
- Android自定义View实现自动吸附功能
- Qt音视频开发35-Onvif图片参数
- alpine安装openssl
- iOS14中的PHPicker
- Android 如何实现动态申请权限
- Android录屏的三种解决方案
- Android 实现将Bitmap 保存到本地
- Android Gradle依赖管理、去除重复依赖、忽略的方式