当前位置:网站首页>PAT 甲级 A 1099 Build A Binary Search Tree
PAT 甲级 A 1099 Build A Binary Search Tree
2022-07-15 14:02:00 【柘木木】
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题意:
给出n个结点的子结点,和n个结点的权重(输入顺序和结点顺序不是一 一对应的,要找到那个权重是那个结点的),求这棵二叉查找树的层序遍历。
思路:
将给的权重从小到大排序就可以得到二叉查找树的中序遍历序列,根据中序遍历的规律,将第一小的值,第二小的值...第n小的值都放到对应的结点,使得该树的结点都有权重,然后再进行层次遍历就好了。
代码:
//属于二叉寻找树中小蝌蚪找妈妈类型
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
//给出的是编号和结点的关系,所以使用二叉查找树的静态写法比较方便;
const int maxn=110;
int weight[maxn];
int index=0,n;
struct node{
int data;
int lchild,rchild;
}Node[maxn];
void inorder(int root){
if(root==-1)return ;//递归边界;
inorder(Node[root].lchild);//在左子树防止第一小,第二小...这样子像小蝌蚪找妈妈一样一个个按中序遍历的顺序找到各自的位置;
Node[root].data=weight[index++];
inorder(Node[root].rchild);
}
int num=0;
void BFS(int root){//结点已经全部赋值,开始层序遍历;
queue<int > q;
q.push(root);
while(!q.empty()){
int top=q.front();
q.pop();
printf("%d",Node[top].data);
num++;
if(num!=n)printf(" ");
if(Node[top].lchild!=-1) q.push(Node[top].lchild);
if(Node[top].rchild!=-1) q.push(Node[top].rchild);
}
}
int main( ){
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d %d",&Node[i].lchild,&Node[i].rchild);//建立二叉树;
for(int i=0;i<n;i++)scanf("%d",&weight[i]);
sort(weight,weight+n);//得到中序遍历序列;
inorder(0);
BFS(0);
return 0;
}边栏推荐
- 技术分享 | 抓包分析 TCP 协议
- 解决运行Mongodb报错 Attempted to create a lock file on a read-only directory: /data/db
- 响应式用户登录表单
- 微信小程序直播插件--获取临时文件(后台集成小程序直播)
- 面试题:fail-safe 机制与 fail-fast 机制分别有什 么作用
- DAY_4作业——判断数组内是否有某一个数据————实现数组映射(放大 10 倍)—— 实现按序插入数组(修改bug)——实现数组去重
- 2022-07-15日报:Meta宣布推出Make-A-Scene:可基于文字和草图控制AI图像生成
- 动圈式扬声器过载过程
- tinymce5.0.8编辑器最新版本中文版
- Responsive user login form
猜你喜欢

开源数据质量解决方案——Apache Griffin入门宝典

ThreadLocal原理及源码解析(一步一步点进去,不要背了,学思想)

Neural network loss and ACC drawing method plot

【第二十二题】地下城与勇士(北理工/北京理工大学/程序设计方法与实践/小学期 )

JS image clipping and compression integration plug-in

Machine learning - matrix derivation basic formula + common formula

BLOOM模型背后的技术实践:1760亿参数模型如何炼成?

工业交换机如何进入web管理界面?

双过程理论与三重心智模型

《天天数学》连载60:二月二十九日
随机推荐
Leetcode-31-next spread
Interprocess communication -- signal principle and detailed explanation
JVM memory model -- Characteristics and functions of runtime data area
创意丝带样式登录页面
Write it down once Net analysis of memory leakage in web system of a power plant
tinymce5.0.8编辑器最新版本中文版
Maximum sub segment and + segment tree one
MGRE comprehensive experiment
【数值分析练习】数值积分(复化梯形,复化Simpson,Romberg积分)C with STL实现
技术分享 | 使用 cURL 发送请求
JVM garbage collection -- how to determine whether an object is garbage
#导入Word文档图片# 阻塞与非阻塞IO操作
js图片编辑器插件Filerobot
Dynamic loudspeaker overload process
微信小程序直播插件--获取临时文件(后台集成小程序直播)
源码中的modCount是什么?有什么作用
Class的生命周期
leetcode 605. Can Place Flowers 种花问题 (简单)
JS figure guess fruit and vegetable games code
There are three ways to create in Polkadot - parallel chain, parallel thread, and smart contract