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的更多相关文章

  1. 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 ...

  2. PAT甲级——A1099 Build A Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  3. PAT_A1099#Build A Binary Search Tree

    Source: PAT A1099 Build A Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recur ...

  4. PAT1099:Build A Binary Search Tree

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  5. 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 ...

  6. 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 ...

  7. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  8. pat1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  9. pat 甲级 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

随机推荐

  1. WPF中如何为ItemsControl添加ScrollViewer并显示ScrollBar

    今天在开发的过程中突然碰到了一个问题,本来的意图是想当ItemsControl中加载的Item达到一定数量时,会出现ScrollViewer并出现垂直的滚动条,但是实际上并不能够达成目标,对于熟手来说 ...

  2. DNS_PROBE_FINISHED_NXDOMAIN & MacOS

    DNS_PROBE_FINISHED_NXDOMAIN 内网 DNS bug 8.8.8.8 8.8.4.4 # new inner Wi-Fi 10.1.3.10 10.1.3.13 Windows ...

  3. PHP namespace、require、use区别

    假设 有文件a.php 代码 <?php class a{//类a public function afun()//函数afun { echo "aaaa"; } } ?&g ...

  4. Python——POP3邮件协议

    一.POP3协议用于收取邮件 二.POP3协议常用方法 user(login):想服务器发送登录名,并显示服务器的响应,表示服务器正在等待该用户的输入密码 pass_(passwd):在用户使用use ...

  5. HTTP协议 - 基础认识

    在http协议使用场景上我们最熟悉的可能就是浏览器了,作为本系列第一篇,就讲一个问题  ”浏览器怎么连接上服务器并获取网页内容的“ : 首先 浏览器怎么连接上服务器的? 如果对OSI七层模型或者TCP ...

  6. 跳转语句之continue

    js里面有两个跳转语句,一个是continue,一个是break.由于这两个跳转语句都是用于循环当中,因此他们也就只能用于while.for.do…while语句中,当然了,break多加一个swit ...

  7. 11.ingress服务

    kubernetes  的service服务我们提到过.service 可以用nodePort的方式和调用公有云LBAAS服务 来对于集群外的client提供服务访问,但是service是工作的osi ...

  8. c提取文件路径、文件名和后缀名

    /* MAKEPATH.C */ #include <stdlib.h> #include <stdio.h> void main( void ) { char path_bu ...

  9. Logging - MVC Using Log4net Save to File and Database

    第一步:创建Config文件夹和log4net.config 第二步:在log4net.confg黏贴以下配置 <?xml version="1.0" encoding=&q ...

  10. Django restframe 视图函数以及ModelSerializer的使用

    建立model数据库 from django.db import models __all__ = ['Book', 'Publisher', 'Author'] # Create your mode ...