PAT_A1099#Build A Binary Search Tree
Source:
Description:
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 (≤) 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 − 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
Keys:
- 二叉树的建立
- 二叉树的遍历
- 二叉查找树(Binary Search Tree)
Code:
/*
Data: 2019-06-26 16:22:18
Problem: PAT_A1099#Build A Binary Search Tree
AC: 21:23 题目大意:
BST定义:lchild < root <= rchild
给定一棵BST的树形,将所给序列填入BST中,并打印层次遍历
输入:
第一行给出,结点数N<=100
接下来N行,给出第i号结点,左孩子的序号,右孩子的序号(0~N-1,-1为空,0为根)
最后一行,N个数
输出:
层次遍历 基本思路:
建立静态树,构建根结点与左右孩子之间的关系
中序遍历静态树,由于BST的中序遍历就是从小到大递增的序列,把排序好的序列依次填入树的结点中
层次遍历并打印输出
*/
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int M=;
priority_queue<int,vector<int>,greater<int> > bst;
struct node
{
int data;
int lchild,rchild;
}tree[M]; void InOrder(int root)
{
if(root == -)
return;
InOrder(tree[root].lchild);
tree[root].data = bst.top();
bst.pop();
InOrder(tree[root].rchild);
} void Travel(int root, int len)
{
queue<int> q;
q.push(root);
while(!q.empty())
{
root = q.front();
q.pop();
printf("%d%c", tree[root].data, --len==?'\n':' ');
if(tree[root].lchild != -)
q.push(tree[root].lchild);
if(tree[root].rchild != -)
q.push(tree[root].rchild);
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,x;
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", &x);
bst.push(x);
}
InOrder();
Travel(,n); return ;
}
PAT_A1099#Build A Binary Search Tree的更多相关文章
- 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 ...
- PAT-1099(Build A Binary Search Tree)Java实现+二叉排序树的中序遍历和层次遍历
Build A Binary Search Tree PAT-1099 本题有意思的一个点就是:题目已经给出了一颗排序二叉树的结构,需要根据这个结构和中序遍历序列重构一棵二叉排序树. 解法:可以根据中 ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- A1099. Build A Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- AcWing 101. 最高的牛 (差分) 打卡
有 NN 头牛站成一行,被编队为1.2.3…N,每头牛的身高都为整数. 当且仅当两头牛中间的牛身高都比它们矮时,两头牛方可看到对方. 现在,我们只知道其中最高的牛是第 PP 头,它的身高是 HH ,剩 ...
- python 内置模块--collections
1.计数器(counter) Counter是对字典的补充,用于追踪值出现的次数. Counter具有字典的全部属性和自己的属性. >>>import collections obj ...
- P1537 弹珠
P1537 弹珠 题目描述 玛莎和比尔各自有自己的弹珠收藏.他们想重新分配收藏品,使两人能平等拥有弹珠.如果所有的弹珠的价值相同,那么他们就可以平分.但不幸的是,有一些弹珠更大,或者更美丽,所以,玛莎 ...
- 红黑数之原理分析及C语言实现
目录: 1.红黑树简介(概念,特征,用途) 2.红黑树的C语言实现(树形结构,添加,旋转) 3.部分面试题() 1.红黑树简介 1.1 红黑树概念 红黑树(Red-Black Tree,简称R-B T ...
- Application.GetOpenFilename 使用说明
Application.GetOpenFilename(FileFilter, FilterIndex, Title, ButtonText, MultiSelect) 语法: 名称 ...
- 第七篇 css选择器实现字段解析
CSS选择器的作用实际和xpath的一样,都是为了定位具体的元素 举例我要爬取下面这个页面的标题 In []: title = response.css(".entry-header h1& ...
- C语言结构体和函数
#include <stdio.h> struct Person { char *name; }; void change1(struct Person p); void change2( ...
- TOP和PS aux命令显示出来的栏目所代表的意思
USER: 行程拥有者 PID: pid %CPU: 占用的 CPU 使用率 %MEM: 占用的记忆体使用率 VSZ: 占用的虚拟记忆体大小 RSS: 占用的记忆体大小 TTY: 终端的次要装置号码 ...
- thrift 的一些相关知识
thrift是一个很好用的跨语言的rpc框架. 但是其也有一些需要注意的问题: 第一: 发现其对于类型检查没有那么严格: 最近工作中发现是可以把一个int类型直接付给string,而没有任何wa ...
- day01 python起源 介绍 解释器 变量 流程控制if
day01 python 一.python的起源 1.python简介 java 企业级应用, android, app c 操作系统, 做开发语言的, 游戏的内核 ...
