分析:

  考察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. UVA 11082 矩阵解压(网络流建模)

    矩阵解压 紫书P374 建模真的是挺难的,如果直接给我这题,我是想不到用网络流的,所以还应多做网路流建模,学会如何转化成网络流 还有,现在用的EK算法是比较慢的,还应去看看Dnic和ISAP,并且理解 ...

  2. properties文件简介及其常用Java操作

    一.properties文件简介 java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值&q ...

  3. Android 日常开发总结的技术经验 60 条

    1. 全部Activity可继承自BaseActivity,便于统一风格与处理公共事件,构建对话框统一构建器的建立,万一需要整体变动,一处修改到处有效. 2. 数据库表段字段常量和SQL逻辑分离,更清 ...

  4. JNI日志调试LOG和中文乱码

    添加日志: 1. 增加log支持. Android.mk文件增加LOCAL_LDLIBS += -llog 2. C代码中增加(放在最前面) #include <android/log.h> ...

  5. java中string stringbuilder stringbuffer 的区别

    1. String 类 String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间. String a = "a&qu ...

  6. 转:Tomcat安装配置及站点说明

    原文地址:http://www.cnblogs.com/Johness/archive/2012/07/20/2600937.html 1.首先是Tomcat的获取和安装. 获取当然得上Apache的 ...

  7. 【转】Using Gamma 2.2

    This is a detailed description of the work with Gamma 2.2. If you are only interested in exact instr ...

  8. Android开发--布局

    一:LinearLayout  1.线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下罗列在屏幕上.每一个LinearLayout里面又可分为垂直布局(android:orie ...

  9. NKUI框架使用

    使用条件: css添加引用: <link rel="stylesheet" href="$rootPath/themes/default/css/tools/nku ...

  10. node,不懂不懂

    Four Day-------------------------node.js分对象全局/核心模块/文件模块path(核心模块)--作用:操作路径basername/获取传入路劲dimame/获取传 ...