PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30)
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 这道题考察的点是二叉搜索树和完全二叉树。刚开始我第一反应就是完全二叉树的性质:用数组存储的话,父节点下标为i,左孩子为2i,右孩子为2i+1。而一个完全二叉搜索树的最小节点肯定在最左边。我一开始的想法是用数组存储,找到最左边那个位置,放进去0,然后找到0的父节点,放1,再放右节点的2,再往上一直放,但是当时没想到用递归,觉得这样写会很麻烦,所以放弃了。
接着我用了最普通的链式方法建树,然后层序遍历输出。不得不说,链式方法建树很繁琐,我调试了很久才通过。具体思路是,先把输入数据放在一个vector里面,然后排序,从小到大排。然后找出整个树的根节点的下标(找的方法是先计算左子树有几个节点),再递归,在左子树再建树。这样非常麻烦,写递归的时候很容易出错。
后来AC之后在网上搜了搜,发现有非常好的做法。其实我一开始的想法类似,但是没有更进一步去想。一个完全二叉搜索树的中序遍历的结果就是递增排列的,那么我们采用中序遍历的方法去建树(即遍历的时候,visit操作是给节点赋值!)。这种逆向思维是我之前完全没想到的,这次学习了。总之就是,采用中序遍历的方法,利用完全二叉树的父子节点关系去建树,最后把数组按序输出即可。
下面我把两种方法的代码都贴上来。很明显,第一段代码明显比第二段简单得多得多得多得多!!!
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; int N;
int pos=;
int *tree;
vector<int> vec; void build(int n)
{
if (n>N) return;
else
{
build(n*);
tree[n] = vec[pos++];
build(n*+);
}
} int main()
{
int element;
cin >> N;
tree = new int [N+];
//输入元素并排序
for (int i=;i<N;i++)
{
cin >> element;
vec.push_back(element);
}
sort(vec.begin(),vec.end()); build();
cout << tree[];
for (int i=;i<=N;i++)
cout << ' ' << tree[i];
return ;
}
#include<iostream>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std; typedef struct node* tree;
struct node
{
int data;
tree left;
tree right;
}; tree BuildTree (tree,int,unsigned,unsigned);
int FindRoot(int,int);
void LevelOrderTraversal(tree T);
vector<int> vec; int main()
{
int N,element;
cin >> N; //输入元素并排序
for (int i=;i<N;i++)
{
cin >> element;
vec.push_back(element);
}
sort(vec.begin(),vec.end()); unsigned b=,e=vec.size()-; int root=FindRoot(N,);
tree T = nullptr;
T=BuildTree(T,root,b,e); LevelOrderTraversal(T); return ;
} int FindRoot(int N,int base)
{
int level=int(log(double(N))/log(2.0))+; //共有这么多层
int root=;
if (N == )
root = ;
else if (N == )
root = ;
else if (N==)
root = ;
else
if (N-(pow(double(),double(level-))-) > pow(double(),double(level-)) )//左子树满了
root = pow(double(),double(level-))-;
else
root = pow(double(),double(level-))-+N-(pow(double(),double(level-))-);
return root+base;
} tree BuildTree(tree T,int root,unsigned b,unsigned e)
{
//cout << b << e << endl;
if (e==b)
{
T=new node;
T->data = vec[b];
T->left = nullptr;
T->right = nullptr;
}
else
{
T = new node;
T->data = vec[root];
T->left = BuildTree(T,FindRoot(root-b,b),b,root-);
if (e!=root)
T->right= BuildTree(T,FindRoot(e-root,root+),root+,e);
else
T->right = nullptr;
}
return T;
} void LevelOrderTraversal(tree T)
{
bool flag=true;
queue<tree> Q;
if (!T) return;
Q.push(T);
while (!Q.empty())
{
if (flag)
{
cout << Q.front()->data;
flag = false;
}
else
cout << ' ' << Q.front()->data;
if (Q.front()->left)
Q.push(Q.front()->left);
if (Q.front()->right)
Q.push(Q.front()->right);
Q.pop();
}
}
PAT题库-1064. Complete Binary Search Tree (30)的更多相关文章
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水
由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...
- 【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& ...
- 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 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 ...
随机推荐
- C语言课程学习的总结
C语言课程学习的总结 学习C程序这门课一年了,这是我们学的第一门专业课.在大学里,C语言不但是计算机专业的必修课程而且也是非计算机专业学习计算机基础的一门必修课程.所以作为我这个计算机专业的学生来说当 ...
- HTML5的入门与深入理解
HTML5是对HTML的第5次重大的修改,虽然HTML5 标准还在制定中,但不能阻碍其势不可挡的脚步,不用HTML5你就OUT了.HTML5与我们常用的HTML4有什么区别呢? 首先要说的是不是所有的 ...
- JSON简介
JSON的全称是JavaScript Object Notion,即JavaScript对象符号,它是一种轻量级的数据交换格式,JSON的数据格式既适合人来读/写,也适合计算机本身解析和生成.最早 ...
- 关于XE10下Indy发送字符串编码的问题
在与硬件对接的过程中,之前用D7环境下的UDPServer.Post发送的指令,硬件可正常识别并正常显示, 后来使用到XE10,重新编译之前的源码,发现所有汉字乱码显示了: 后通过对接收数据发现,实际 ...
- nginx 页面乱码问题
在配置nginx时常常遇到网页乱码的问题如图: 这时需要在server段里面添加两行: default_type 'text/html'; charset utf-8; 然后执行测试 重启操作 ng ...
- c++内存对齐
内存对齐原则: 1.数据成员对齐规则:struct, union的数据成员,第一个数据成员放在offset为0的地方,之后的数据成员的存储起始位置都是放在该数据成员大小的整数倍位置.如在32bit的机 ...
- SharePoint Calculated Columns 分类: Sharepoint 2015-07-09 01:49 8人阅读 评论(0) 收藏
SharePoint Calculated Columns are powerful tools when creating out-of-the-box solutions. With these ...
- MS SQL提示列名 'Y' 无效的原因及解决办法
在作项目写MS SQL 存储过程时,需拼接SQL语句字符串,其中有单字符变量,如下图: 如上图执行存储过程是提示“列名‘Y’无效”.经反复测试,原因在用单字符变量连接SQL字符串是必须在引用变量前后各 ...
- No module named * 但是已经安装了找不到解决办法
错误现象,把\Lib\site-packages下的安装包挪动位置到新的位置后(多个Python.exe运行位置)引起这类错误 No module named * 但是已经安装了 解决过程,先试图卸载 ...
- cocoapods无法使用(mac os 10.11升级导致pod: command not found)
之前安装了cocoapods, 那么输入 : sudo gem install -n /usr/local/bin cocoapods 如果还不行的话 首先在终端输入 gem sources -l 查 ...