分析:

  考察BST + 完全二叉树的性质,注意:

    (1):先用排序排好,然后由于是完全二叉树,我们使用中序来建树。

    (2):建好之后,层次遍历可以采用队列。

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath> using namespace std; vector<int> vect;
vector<int> result; struct Node
{
int value;
Node *left;
Node *right;
}*root; Node *In(int level, int ll, int rr)
{
if (ll > rr) return NULL;
int up_level = pow(, (level - )) - ; //除最后一层外 总共有多少个
int Last_level_left = (rr - ll) + - up_level; //最后一层剩下的 int k = pow(, (level - ) - ); //最后一层是否布满了左分支 int left_left;
if (Last_level_left >= k) left_left = up_level;
else left_left = pow(, (level - )) - + Last_level_left; Node *p = new Node();
p->value = vect[left_left + ll]; p->left = In(level - , ll, ll + left_left - );
p->right = In(level - , ll + left_left + , rr); return p;
} void level_order(Node *root)
{
queue<Node *> q;
q.push(root); while (!q.empty())
{
Node *p = q.front();
q.pop(); result.push_back(p->value); if (p->left != NULL)
q.push(p->left);
if (p->right != NULL)
q.push(p->right);
} printf("%d", result[]);
for (int i = ; i < result.size(); i++)
printf(" %d", result[i]);
printf("\n");
} int main()
{
int n; while (cin >> n)
{
vect.clear();
result.clear();
for (int i = ; i < n; i++)
{
int k;
cin >> k;
vect.push_back(k);
}
sort(vect.begin(), vect.end()); int level = log2(n) + ; root = In(level, , n - );
level_order(root); }
return ;
}

1064. Complete Binary Search Tree (30)的更多相关文章

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

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

  2. pat 甲级 1064. Complete Binary Search Tree (30)

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

  3. 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

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

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

  5. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

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

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

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

  8. 【PAT甲级】1064 Complete Binary Search Tree (30 分)

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

  9. PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

    因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...

随机推荐

  1. java io 输入输出流

    数据流分类: 流序列中的数据既可以是未经加工的原始二进制数据, 也可以是经一定编码处理后符合某种格式规定的特定数据. 因此Java中的流分为两种: 1) 字节流:数据流中最小的数据单元是字节 2) 字 ...

  2. Oracle linux6.1配置yum本地源

    一.准备工作1. Linux安装盘插入光驱2. 挂载光驱 [root@localhost ~]# mount /dev/cdrom /mnt/ mount: block device /dev/sr0 ...

  3. Python 半开放socket

    利用shutdown实现半开放的socket #server.py import socket s=socket.socket() s.bind(('127.0.0.1',2000)) s.liste ...

  4. 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  5. android C++环境搭建

    http://jingyan.baidu.com/article/3ea51489e7a9bd52e61bbac7.html

  6. 使用gson解析,生成Json

    包:gson-2.3.jarJson文本解析为Java对象:Java对象生成为Json文本 import com.google.gson.Gson; public class TestGson { c ...

  7. 本机搭建外网web服务器

    版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 首先声明一下楼主是个开发人员,按理说这些搭建服务器什么的,和楼主半毛钱的关系都没有.但是呢,楼主是个爱学习的人,懂得德智体全面 ...

  8. spark1.5.1环境搭建

    今天一个小伙伴找我搞spark,搞了一个中午都没搭建好.心里痒痒的.索性自己重来了一遍. 本来很简单的事情,被这小子搞的老复杂了.究其原因,有以下几点: 下载的软件不对,我用的是这两个软件 spark ...

  9. 往sql数据库表中添加字段

    通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数增加字段: alter table [表名] add 字段名 smallin ...

  10. 【原】JS正则表达式里的控制符

    正则表达式易于使用而又让人费解,乍一看上去,就像是一行行的乱码,但是它的功能确实又不容小觑.今天整理正则时,纠正了自己的一个误解. 先缕一缕: 正则表达式的两种声明方式: 字面量.构造器 (RegEx ...