http://www.patest.cn/contests/pat-a-practise/1099

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
此题是2015年春季的研究生入学考试复试时的机试题,链接 http://www.patest.cn/contests/graduate-entrance-exam-2015-03-20

这道题的考点有:树的构造、中序遍历(BST,递归)、层序遍历(层序输出,队列)、排序(怎么省事怎么来)。
可能看第一遍时觉得挺复杂的,但是如果树的遍历掌握的话,只要列出处理流程就很简单明了了。 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树):
它或者是一棵空树,或者是具有下列性质的二叉树:
若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉排序树。 本题给出节点的左右孩子的下标和一些整型数据,要求构造出一个BST,然后层序输出这颗二叉树上所有节点的key值。 第一步,我们需要构造这棵BST,以便数据归位。实际上,这些节点本身就代表这棵二叉树,这一步使我们在头脑中(概念上)构造二叉树,不需要敲代码实现,考察的对二叉树的理解。
第二步,数据是无序的,所以为了数据归位,需要进行排序。数据量很小,不超过100,所以怎么简单怎么排,反正内存大小和时间足足的.
第三步,进行数据归位。根据bst的性质,其实就是中序处理每个节点。
第四步,按要求层序输出即可。 总结:输入->keys排序->中序遍历,key值归位->层序遍历,格式输出。
 #include<cstdio>
#include<cstring>
// a positive integer N (<=100)
int num=,node[][]={{,,}},keys[]={},keyloc=;
void levelprint()
{
int level[]={},start=,len=;
level[start]=,len++;
while(len)
{
if(node[level[start]][]!=-)//左子树
level[start+len]=node[level[start]][],len++;
if(node[level[start]][]!=-)//左子树
level[start+len]=node[level[start]][],len++; if(start) printf(" ");
printf("%d",node[level[start]][]);
start++,len--;
} }
void inorder(int rootloc)
{
if(node[rootloc][]!=-)//左子树
inorder(node[rootloc][]);
node[rootloc][]=keys[keyloc],keyloc++; //本节点
// printf("\n %d %d %d %d",rootloc,node[rootloc][0],node[rootloc][1],node[rootloc][2]);
if(node[rootloc][]!=-)//右子树
inorder(node[rootloc][]);
}
void keyssort()
{
for(int i=;i<num;i++)
{
int key=keys[i],loc=i-;
for(;loc>=;loc--) if(keys[loc]<=key) break;
loc++;
for(int j=i;j>loc;j--) keys[j]=keys[j-];
keys[loc]=key;
}
}
int main()
{
scanf("%d",&num);
for(int i=;i<num;i++) scanf("%d%d",&node[i][],&node[i][]);
for(int i=;i<num;i++) scanf("%d",keys+i);
keyssort();//keys排序 升序
inorder();//中序遍历,keys归位
levelprint();//层序输出
return ;
}

PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)的更多相关文章

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

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

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

  4. Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

    Pat1043代码 题目描写叙述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the f ...

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

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

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

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

  7. PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)

    简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...

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

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

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

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

随机推荐

  1. Event事件的三个阶段

    转自www.w3school.com.cn/htmldom/event_bubbles.asp 在 2 级 DOM标准中,事件传播分为三个阶段: 第一,捕获阶段.事件从 Document 对象沿着文档 ...

  2. 无法序列化会话状态。请注意,当会话状态模式为“StateServer”或“SQLServer”时,不允许使用无法序列化的对象或 MarshalByRef 对象。

    原文链接:http://blog.csdn.net/byondocean/article/details/7564502 session是工作在你的应用程序进程中的.asp.net进程.iis往往会在 ...

  3. Server.MapPath()相关

    Server.MapPath()相关 1.      Server.MapPath()介绍 Server.MapPath(string path)作用是返回与Web服务器上的指定虚拟路径相对应的物理文 ...

  4. 51nod1181【素数筛】

    思路: 直接就是筛出素数,然后我很撒比的从那个地方往后for找一个位置也是质数的输出: #include <bits/stdc++.h> using namespace std; type ...

  5. Modulation of Lipid Metabolism by Celastrol (文献分享一组-赵倩倩)

    文献名:Modulation of Lipid Metabolism by Celastrol (雷公藤红素对脂质代谢调节作用的研究) 期刊名:Journal of Proteome Research ...

  6. 【微服务】Dubbo初体验

    一.前言 之前微服务这块只用过SpringCloud搭建,但是最近面试会被问到dubbo框架,虽然之前也学了但是都忘了,故写此博客加深印象. 二.原理简介 Dubbo是一个分布式服务框架,以及阿里巴巴 ...

  7. C 语言实例 - 将字符串写入文件

    C 语言实例 - 将字符串写入文件 C 语言实例 C 语言实例 将字符串写入文件. 实例 #include <stdio.h> #include <stdlib.h> /* e ...

  8. ACM2015沈阳:B-Bazinga

    2019.1.24 数据范围:\(n<=500,m<=2000\) 这个题最裸的暴力就是对于每个字符串,都去验证一次,时间复杂度\(O(n^2m)\) 我们发现,如果对于字符串\(i\), ...

  9. Php对象及对象特性篇

    前言 以前写C++最多,大二课上学过Java.现在也差不多还给老师了.现在决定针对php重新梳理一遍,夯实基础,设计模式学起来应该会更加得心应手吧. 介绍 对象作为数据和功能代码的集合,是程序开发和代 ...

  10. CentOS Linux 搭建 SVN(CollabNet Subversion)服务器

    安装CollabNet Subversion之前必须先安装JDK1.6和python2.4 ~ 2.6 groupadd svn useradd -g svn svnuser  passwd svnu ...