04-树5 Complete Binary Search Tree
这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04.html)来的简单,不过老师给的思路是一种挺通用的思路,可以用来解决一系列的问题,但我目前看着有点吃力。我坚持认为对全局变量的使用需十分谨慎,能不用就不用,所以为了不出现全局变量,就无辜多了一串参数。实现代码如下,题目在代码下方
#include <stdio.h>
#include <stdlib.h> int compare(const void * a, const void * b);
void inOrder(int * a, int n, int * in, int N); int main()
{
// freopen("in.txt", "r", stdin); // for test
int i, N, n;
scanf("%d", &N);
int a[N];
for(i = ; i < N; i++)
{
scanf("%d", &n);
a[i] = n;
} qsort(a, N, sizeof(int), compare);
int in[N + ];
inOrder(a, , in, N);
for(i = ; i <= N; i++)
{
printf("%d", in[i]);
if(i < N)
printf(" ");
else
printf("\n");
}
// fclose(stdin); // for test
return ;
} int compare(const void * a, const void * b)
{
return *(int *)a - *(int *)b;
} void inOrder(int * a, int n, int * in, int N)
{
static int i = ; if(n * <= N)
inOrder(a, * n, in, N);
in[n] = a[i++];
if(n * + <= N)
inOrder(a, n * + , in, N);
}
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.
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.
Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input:
10
1 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
04-树5 Complete Binary Search Tree的更多相关文章
- PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- A1064. Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- pat04-树8. Complete Binary Search Tree (30)
04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT_A1064#Complete Binary Search Tree
Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...
- PAT甲级——A1064 Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- Windows7睡眠后自动唤醒
笔者的电脑(Windows7 64位旗舰版)睡眠后,隔段时间后会自动唤醒.经两项配置后,解决了该问题. 1 禁用唤醒定时器 控制面板里进入"电源选项""更改计划设置&qu ...
- tcp服务的测试程序开源
开源的是一个测试客户端,可以用来检验服务器端程序的并发处理能力. 使用方法为: python epolltest.py --host=192.168.15.128 --port=8809 --degr ...
- Xml反序列化
XML的反序列化可在类的属性上标记特性来隐射反序列化.例如这种形式 public class PaymentAccount { [XmlAttribute("name")] pub ...
- 【bzoj1040】骑士
[bzoj1040]骑士 题意 给定一个基环森林,求最大独立集. 分析 其实这是一道一年前做过的题. 只是今天在看bzoj1023的时候突然来了几许兴致,回过头来看一看. 如果对于一棵树的最大独立集, ...
- Java并发编程:并发容器之CopyOnWriteArrayList
转载: Java并发编程:并发容器之CopyOnWriteArrayList Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个 ...
- gulp-webpack工程化实现electron
参考网站: https://www.npmjs.com/package/babel-loader https://www.npmjs.com/package/gulp https://www.npmj ...
- Django开发博客- 三部曲
其实在django中实现一个功能只需要三个步骤即可,这里我姑且叫它三部曲. 这三部曲就是: 定义urls映射 定义views 定义templates 什么是URL? URL就算一个WEB地址,你在浏览 ...
- centos svn服务器安装
1.安装必须的软件 yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql 2.创建代码库 mkdir -p /root ...
- each用法
1.数组用法 <script> var s=["s","i","l","e","n",& ...
- js(引用类型和setTimeout scope)
题目是群中小伙伴出的. var a = [1,2,3]; c= a //todo 限制条件 c 不能出现在 = 左边 console.log(a) console.log(c) console.log ...