这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(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的更多相关文章

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

  2. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  3. A1064. Complete Binary Search Tree

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

  4. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  5. pat04-树8. Complete Binary Search Tree (30)

    04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...

  6. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  7. PAT_A1064#Complete Binary Search Tree

    Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...

  8. PAT甲级——A1064 Complete Binary Search Tree

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

  9. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

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

随机推荐

  1. 在Fedora 20下使用TexturePacker

    TexturePacker应该是最流行的图片合并工具吧,它把多个小图组合成一个大图,以减少网络请求次数,还有利于内存的充分利用.在游戏开发和网页开发时经常会用到它,CanTK(https://gith ...

  2. 2014---多校训练一(A Couple doubi)

    Couple doubi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. json2.js 使用

    参考:http://www.cnblogs.com/youring2/archive/2013/03/01/2938850.html github地址:https://github.com/dougl ...

  4. Laravel

    Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁.富于 ...

  5. java并发带返回结果的批量任务执行

    转载:http://www.it165.net/pro/html/201405/14551.html 一般情况下,我们使用Runnable作为基本的任务表示形式,但是Runnable是一种有很大局限的 ...

  6. Icon资源详解[1]

    本文分享&备忘最近了解到的icon资源在windows平台下相关的一部分知识,所有测试代码都尽可能的依赖win32 API实现.更全面的知识,参考文末列出的”参考资料“.      关键字:I ...

  7. DOM系列---基础篇[转]

    DOM (Document Object Model) 即文档对象模型, 针对 HTML 和 XML 文档的 API (应用程序接口) .DOM 描绘了一个层次化的节点树,运行开发人员添加.移除和修改 ...

  8. [整][转]Invoke和BeginInvoke的使用

    在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate. 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是do ...

  9. android 启动时的短暂黑屏解决

    http://blog.csdn.net/lonely_fireworks/article/details/22291835 http://www.cnblogs.com/henanjing/arch ...

  10. jquery 字数限制

    $("#TextArea1").keydown(function(){ 10 var curLength=$("#TextArea1").val().lengt ...