1099. Build A Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

  1. PAT1099:Build A Binary Search Tree

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

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

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

  3. 1099. Build A Binary Search Tree (30)

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

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

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

  6. PAT-1099(Build A Binary Search Tree)

    题目见这里 分析:分四步进行 1)根据给定的结点情况建二叉树  2)对输入的键值排序(asending) 3)对二叉树中序遍历,同时对应赋key值 4)层次遍历(队列应用) 题目并不困难,但是我误入了 ...

  7. PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

    预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...

  8. PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

    题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...

  9. 【PAT甲级】1099 Build A Binary Search Tree (30 分)

    题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...

随机推荐

  1. 九 fork/join CompletableFuture

    1: Fork/join fork/join:  fork是分叉的意思, join是合并的意思. Fork/Join框架:是JAVA7提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务 ...

  2. 【转】深刻理解render 和 redirect_to

    由于最近老是在表单提交后出现没有反应的现象,发现是在action中的使用render 和 redirect_to的原因,于是就想搞清楚他两真正的区别在哪里,上一遍的blog也谈到了这二者的区别,但是有 ...

  3. 四种生成和解析XML文档的方法介绍

    解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 1.DOM(Document Object Model) DOM是用与平台和语言无关的方式表示XML文档的官方 ...

  4. iOS开发者福利之精品源码汇总!免费下载

    汇总一些看着不错的源码,有需要的朋友过来下载吧!{:4_102:} 1.用swift制作的色彩炫丽的进度条-KDCircularProgressKDCircularProgress是使用swift制作 ...

  5. centos6.5安装zookeeper教程(三)

    阅读前建议先阅读: http://www.cnblogs.com/duenboa/articles/6665159.html   1. 下载安装文件zookeeper-3.4.6.tar.gz 镜像地 ...

  6. linux 下 安装mysql

    安装之前,因为redhat 是yum自带的,但是不能使用,因为要交钱,还要订阅,所以需要卸载,重新安装163提供的yum 在另外一篇文章介绍 yum list mysql* 列出所有关于mysal的安 ...

  7. Ensembl突变数据描述之(一)——突变物种数据库及预测工具

    以下是对Ensembl突变数据库中储存的数据的描述,对于Ensembl数据库中不同的物种,我们从各种来源(例如,dbSNP数据库)导入突变数据(SNP.CNV.等位基因频率.基因型等),导入的突变数据 ...

  8. 1. csrf 简介

    浅谈CSRF CSRF是什么? (Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,它在 年曾被列为互联网 大安全隐患之一,也被称为“One Click A ...

  9. AutoCAD 2014 在Server 2008 中 十字光标很慢

    另外,请在你们的Windows Server上增加如下注册表: On      both Windows Server 2008 R2 and Windows Server 2012 create E ...

  10. 微信小程序自学第五课:条件渲染、列表渲染

    一.条件渲染 1. wx:if 在框架中,我们用 wx:if="{{condition}}" 来判断是否需要渲染该代码块: <view wx:if="{{condi ...