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& ...
随机推荐
- Java 窗体的基本操作语句 JFrame
package com.swift; import java.awt.Color; import java.awt.GridLayout; import java.util.Random; impor ...
- IDEA注释模板设置
1. 文件注释 1.1 Java类文件注释 Setting -> Editor -> File and Code Templates -> Includes -> File H ...
- Ubuntu设置代理上网
代理服务器(Proxy Server)是个人网络和Internet服务商之间的中间代理机构,它负责转发合法的网络信息,对转发进行控制和登记.代理服务器作为连接Internet(广域网)与Intrane ...
- ZendFramework-2.4 源代码 - 关于MVC - Model层
所谓的谓词Predicate // ------ 所谓的谓词 ------ // 条件 case.3 $where = new \Zend\Db\Sql\Where(); $expression = ...
- 27.28. VUE学习之--事件修饰符之stop&capture&self&once实例详解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 调用python-nmap实现扫描局域网存活主机
使用环境:Raspberry 3b+ +netifaces+python-nmap+nmap 调用netifaces自动获取ip地址: def get_gateways(): return netif ...
- day 52 Django基础一之web框架的本质
Django基础一之web框架的本质 django第一天 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于D ...
- [译]The Python Tutorial#7. Input and Output
[译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...
- makefile学习(2)
新建目录如下: ├─include │ integrate.h │ └─src │ integrate.c │ main.c │ makefile │ └─obj obj用于存放object文件. m ...
- Kilani and the Game CodeForces - 1105D (bfs)
Kilani is playing a game with his friends. This game can be represented as a grid of size n×mn×m, wh ...