pat 甲级 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 42Sample Output:
58 25 82 11 38 67 45 73 42
题意:给定n个数插入BST,并且插入数值后的BST结构已经确定,依据这个结构来推断BST上每个节点对应的数值,并且层序遍历BST。
思路:一开始的思路比较复杂,将n各数值从小到大排列,我先搜索出了BST上每个节点左右子树的节点个数。在此基础上就可以递推的确定每个节点对应的数值在数列上的位置。假设
已经确定某一个节点x对应的数值在数列上的位置pos,那么其节点x的左儿子的数值所在位置与pos的距离间隔就是左儿子的右子树节点个数(原因:比左儿子的数值大又比x的数值小,这些节点的数值当然都存储在左儿子的右子树上),从而可以推断左儿子的数值。右儿子的数值推断方法类似。
但其实不需要如此复杂,按照中序遍历的顺序就可以直接推断出各个节点上对应的数值。。。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 100+5
int n;
struct Node {
int l_child, r_child;
int key;
}node[N_MAX];
vector<int>vec;
pair<int, int>num[N_MAX];
/*
int dfs(int x) {//查询每个节点左右儿子的数量
int left_num=0, right_num=0;
if(node[x].l_child!=-1)left_num = dfs(node[x].l_child);
if (node[x].r_child != -1)right_num = dfs(node[x].r_child);
num[x] = make_pair(left_num, right_num);
return right_num + left_num+1;
} void dfs2(int x,int pos) {//节点x上的数值为vec[pos],考虑节点x与儿子节点的距离来推断儿子节点的位置
node[x].key = vec[pos];
int pos_left = pos - num[node[x].l_child].second - 1;
int pos_right = pos + num[node[x].r_child].first + 1;
if(node[x].l_child!=-1)dfs2(node[x].l_child, pos_left);
if(node[x].r_child!=-1)dfs2(node[x].r_child, pos_right);
}*/ int step = ;
void inorder(int x) {//其实按照中序遍历的顺序就可以依次确定每个节点的数值key
if(node[x].l_child!=-)inorder(node[x].l_child);
node[x].key = vec[step++];
if(node[x].r_child!=-)inorder(node[x].r_child);
} int output[N_MAX];
void bfs(int root) {
queue<int>que;
que.push(root);
int cnt = ;
while(!que.empty()) {
int x = que.front(); que.pop();
output[cnt++] = node[x].key;
if(node[x].l_child!=-)que.push(node[x].l_child);
if(node[x].r_child!=-)que.push(node[x].r_child);
}
} int main(){
while (scanf("%d",&n)!=EOF) {
for (int i = ; i < n;i++) {
int l, r; scanf("%d%d",&l,&r);
node[i].l_child = l, node[i].r_child = r;
}
vec.resize(n);
for (int i = ; i < n; i++)scanf("%d",&vec[i]);
sort(vec.begin(),vec.end());
//dfs(0);
//dfs2(0, num[0].first);
inorder();
bfs();
for (int i = ; i < n; i++)printf("%d%c",output[i],i+==n?'\n':' ');
}
return ;
}
pat 甲级 1099. Build A Binary Search Tree (30)的更多相关文章
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- PAT 甲级 1099 Build A Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
- 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 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 ...
- PAT甲级——A1099 Build A Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...
随机推荐
- Dynemic Web Project中使用servlet的 doGet()方法接收来自浏览器客户端发送的add学生信息形成json字符串输出到浏览器并保存到本地磁盘文件
package com.swift.servlet; import java.io.FileOutputStream;import java.io.IOException;import java.io ...
- 解决国内网络Python2.X 3.X PIP安装模块连接超时问题
pip国内的一些镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/si ...
- 【Python学习之十】yield之send方法
yield作用 简单地讲,yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator.下面以斐波拉 ...
- NoSQL - KVstore -Redis
Redis键迁移 在使用Redis的过程中,很多时候我们会遇到需要进行键迁移的问题,需要将指定Redis中的指定数据迁移到其他Redis当中,键迁移有三种方法,我们来进行一一介绍. 一.move mo ...
- 一次完整的HTTP请求需要的7个步骤
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连 ...
- Laravel 打印已执行的sql语句
打开app\Providers\AppServiceProvider.PHP,在boot方法中添加如下内容 5.2以下版本 // 先引入DB use DB; // 或者直接使用 \DB:: DB::l ...
- JZOJ 5344. 摘果子
Description Input Output Sample Input 7 9 39 6 13 2 22 6 7 4 -19 5 28 6 -17 1 2 1 3 2 4 1 5 4 6 2 7 ...
- stm32基本定时器timer6的原理与使用
/********************基本定时器 TIM 参数定义,只限 TIM6.7************/ /* 一.定时器分类 STM32F1 系列中,除了互联型的产品,共有 8 个定时器 ...
- Linux安全层详解
1 bastion(安全堡垒系统)通常配置两个服务: 1 服务定义系统的功能: 2 服务支持远程访问: 原则: 1 不需要某个软件卸载掉: 2 需要某个软件但不使用就不要激活: 为各个bastion系 ...
- np.newaxis()用法
这个是liaspace函数 这个是np.newaxis的用法,增加维度,写一个表示增加一维,两个表示增加2维2位置的:号是对a的取值范围,如果把np.newaxis作为第一个参数是对行增加维度,作为第 ...