A1099. Build A Binary Search Tree
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
#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
bool cmp(int a, int b){
return a < b;
}
typedef struct NODE{
int lchild, rchild;
int key;
}node;
node tree[];
int N, num[], index = ;
void inOrder(int root){
if(root == -)
return;
inOrder(tree[root].lchild);
tree[root].key = num[index++];
inOrder(tree[root].rchild);
}
void levelOrder(int root){
int cnt = ;
queue<int> Q;
if(root != -){
Q.push(root);
}
while(Q.empty() == false){
int temp = Q.front();
Q.pop();
cnt++;
if(cnt == N)
printf("%d", tree[temp].key);
else printf("%d ", tree[temp].key);
if(tree[temp].lchild != -)
Q.push(tree[temp].lchild);
if(tree[temp].rchild != -)
Q.push(tree[temp].rchild);
}
}
int main(){
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d%d", &tree[i].lchild, &tree[i].rchild);
}
for(int i = ; i < N; i++){
scanf("%d", &num[i]);
}
sort(num, num + N, cmp);
inOrder();
levelOrder();
cin >> N;
return ;
}
总结:
1、题意:给出一个二叉树的具体形状,给出一些键值,要求将这些键值按照给定的形状插入,使之成为搜索树。
2、二叉搜索树的中序序列是从小到大的有序序列。根据这一性质,先对序列进行排序,就得到了搜索树的中序序列。再对给出的二叉树进行中序遍历,在遍历的过程中插入keys,就得到了一个搜索树。
A1099. Build A Binary Search Tree的更多相关文章
- PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT甲级——A1099 Build A Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT_A1099#Build A Binary Search Tree
Source: PAT A1099 Build A Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recur ...
- PAT1099:Build A Binary Search Tree
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PAT 1099 Build A Binary Search Tree[BST性质]
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- pat1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
随机推荐
- C# Note4:XML序列化和反序列化(含加密解密等)
前言 在项目中,我们经常用到各种配置文件,比如xml文件.binary文件等等,这里主要根据实践经验介绍下xml文件的序列化和反序列化(毕竟最常用). 实践背景:我要做一个用户管理功能,用户账号信息存 ...
- MyBatis映射文件4(参数获取#{}和${}/select标签详解[返回类型为list])
参数获取 之前我们都是采用#{}的方式进行参数传递,其实MyBatis还有另外的参数传递方式${} 使用方法相同,但是还是有很大区别的 这里做一个测试: <select id="get ...
- Spring Boot+Jsp启动异常
No Java compiler available for configuration options compilerClassName 加入maven配置 <dependency> ...
- cookie路径概念理解
.创建一个cookie并设置 cookie的有效路径: $.cookie('the_cookie', 'the_value', { expires: 7, path: '/' }); 注:在默认情况下 ...
- RPC框架-RMI、RPC和CORBA的区别
关键词:RMI RPC CORBA简 介:本篇文章重点阐述RMI,附带介绍RPC和CORBA Java远程方法调用(Java RMI)是一组实现了远程方法调用(rmi)的API. java RMI是远 ...
- 使用PHP对二维索引数组进行排序
本例中 data 数组中的每个单元表示一个表中的一行.这是典型的数据库记录的数据集合. 例子中的数据如下: volume | edition -------+-------- 67 | 2 86 | ...
- php重定向http请求
302 临时重定向 301 永久重定向 ( 302 和 301 的区别主要在于搜索引擎,搜索引擎一般不会抓取临时重定向的页面 ) 301 和302 适用于 普通的GET 请求: 如果 ...
- Matlab提供了两种除法运算:左除(\)和右除(/)
Matlab提供了两种除法运算:左除(\)和右除(/).一般情况下,x=a\b是方程a*x =b的解,而x=b/a是方程x*a=b的解.例:a=[1 2 3; 4 2 6; 7 4 9]b ...
- Tunnel Warfare(线段树取连续区间)
emmmmmmmm我菜爆了 思路来自:https://blog.csdn.net/chudongfang2015/article/details/52133243 线段树最难的应该就是要维护什么东西 ...
- 使用poi将Excel文件转换为data数据
pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:// ...