pat1099. 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
平衡二叉树。每次找到中间节点。
#include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<queue>
#include<map>
using namespace std;
struct node{
int l,r,v;
};
node tree[];
int line[];
int calnum(int num){
if(num==-){
return ;
}
return calnum(tree[num].l)+calnum(tree[num].r)+;
}
void BuildAVL(int mid,int *line){
if(mid==-){
return ;
}
int count=calnum(tree[mid].l);//统计左子树 //cout<<count<<endl; tree[mid].v=line[count];
BuildAVL(tree[mid].l,line);
BuildAVL(tree[mid].r,line+count+);
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i;
for(i=;i<n;i++){
scanf("%d %d",&tree[i].l,&tree[i].r);
}
for(i=;i<n;i++){
scanf("%d",&line[i]);
}
sort(line,line+n); /*for(i=0;i<n;i++){
cout<<i<<" "<<line[i]<<endl;
}*/ BuildAVL(,line); //cout<<":"<<" "<<tree[0].v<<endl; queue<int> q;
int cur;
q.push();
printf("%d",tree[].v);
while(!q.empty()){
cur=q.front();
q.pop();
//cout<<"cur: "<<cur<<endl; if(tree[cur].l!=-){
q.push(tree[cur].l);
printf(" %d",tree[tree[cur].l].v);
}
if(tree[cur].r!=-){
q.push(tree[cur].r);
printf(" %d",tree[tree[cur].r].v);
}
}
printf("\n");
return ;
}
pat1099. Build A Binary Search Tree (30)的更多相关文章
- 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 (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 (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 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-1099(Build A Binary Search Tree)
题目见这里 分析:分四步进行 1)根据给定的结点情况建二叉树 2)对输入的键值排序(asending) 3)对二叉树中序遍历,同时对应赋key值 4)层次遍历(队列应用) 题目并不困难,但是我误入了 ...
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
随机推荐
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- Jquery隐藏相同name的div
$("div:[name=divName]").hide(); divName(自己div的Name)
- vue.js 使用高德地图
1.获取key值 注册成为高德开发者需要分三步: 第一步,注册高德开发者: 第二步,去控制台创建应用: 第三步,获取Key 2.修改配置文件 webpack.base.conf.js externa ...
- redis持久化 发布消息与订阅
vi /usr/local/redis/etc/redis.conf 快照方式: save 900 1 save 300 10 save 60 10000 aof方式: ap ...
- IO系列之File
1 File类 1.1 目录列表器 在这里我主要是参考Think in Java的内容从而做的一些总结以及扩展.Java中的IO流的设计应该说是Java中最经典的,最学院式的设计,包括它的整体架构设计 ...
- Tomcat+Nginx实现动静分离
Tomcat是我们经常用的服务器,轻便快捷,但是数据量大的时候,会影响访问.响应速度,这时Nginx就出现了. Nginx可做反向代理.负载均衡.动态与静态资源的分离的工作,这里我们就用它来做动静分离 ...
- windows、Linux 测试服务器、电脑的某些个端口是否打开
测试远程端口是否开放包括两种方法: 一. 命令行的形式 二.代码 先参考我的博客 windows.Linux 开放端口 一.命令行的形式 两个命令:telnet.nc(netcat) 两种网络层协议: ...
- hadoop-2.3.0-cdh5.1.0完全分布式搭建(基于centos)
先参考:<hadoop-2.3.0-cdh5.1.0伪分布安装(基于centos)> http://blog.csdn.net/jameshadoop/article/details/39 ...
- Python脚本打包为exe文件
本文转载自http://www.open-open.com/lib/view/open1342675735745.html 把用Python写好的脚本,可以用pyinstaller打包成.exe文 ...
- 8、泛型程序设计与c++标准模板库3.迭代器
理解迭代器对于理解STL框架并掌握STL的使用至关重要.简单地说,迭代器是面向对象版本的指针,STL算法利用迭代器对存储在容器中的元素序列进行遍历,迭代器提供了访问容器和序列中每个元素的方法. 虽然指 ...