1064. Complete Binary Search Tree (30)
分析:
考察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)的更多相关文章
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 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 ...
- 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甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT (Advanced Level) 1064. Complete Binary Search Tree (30)
因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...
随机推荐
- Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptabl
在使用AFNetworking 2.0 的时候本来一切很顺畅,但是中途遇到几个比较坑的地方 这里分享一下爬坑经历,忘读者不能速爬坑! 在发送请求后,NSURLSessionDataTask一直报错 ...
- Java多线程-线程的同步与锁
一.同步问题提出 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏.例如:两个线程ThreadA.ThreadB都操作同一个对象Foo对象,并修改Foo对象上的数据. package ...
- 根据 MySQL 状态优化 ---- 2. 连接数
查看 MySQL 服务器运行的各种状态值: mysql> show global status: 2. 连接数 查看设置的最大连接数: mysql> show variables like ...
- 直接解压msi文件
msiexec /a "F:\TDDownload\subversion-1.5.5.msi" /qb TARGETDIR="F:\TDDownload\subversi ...
- LeetCode Implement pow(x, n).
这个题目我也没有思路,同学们可以查看这个http://www.cnblogs.com/NickyYe/p/4442867.html 下面是我改进后的代码 第一种方法: class Solution { ...
- 【解决】SharePoint集成模式下Reporting Service—为用户授予的权限不足,无法执行此操作。 (rsAccessDenied)
环境:Windows Server 2008 R2 SP1,SharePoint 2010 企业版,SQL Server 2008 R2 Reporting Service(SharePoint集成模 ...
- Chap5: question 35 - 37
35. 第一个只出现一次的字符 char firtNotRepeat(char *s) { if(s == NULL) return 0; int i = 0; while(s[i] != '\0') ...
- The listener supports no services解决一例
The listener supports no services解决一例 Listener动态监听静态监听注册实例 今天做Advacned Replication实验的时候碰到一个问题,启动目标 ...
- SpringMVC整合MongoDB开发 架构搭建
系统环境: 操作系统: windows 7 数 据 库: mongodb2.0.6 驱 动 包: Spring3.1.2 + mongodb2.7.3 + spring-data-mongodb1 ...
- myeclipse自动排版
myeclipse代码排版方式有两种: 1. ctr+f 实现自动排版: 2. myeclipse->Preference->Java->Editor->Sava Action ...