pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)
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
重点在递归的过程建树
详见代码:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[1005];
int cnt[12]={-1,1,3,7,15,31,63,127,255,511,1023};
struct node
{
int v;
node *left=NULL,*right=NULL;
};
int mypow(int a,int b)
{
int res=1;
while(b--)res*=a;
return res;
}
node* build(int l,int r,int num)
{
if(l>r){return NULL;}
if(l==r)
{
node *tt=new node();tt->v=a[l];
return tt;
}
if(num<=0)return NULL;
node *thi;
int k1=0;
while(cnt[k1+1]<num)k1++;
int k2=k1+1;
int rr=num-(cnt[k1]);
int tmp=mypow(2,k2-2);
if(rr>=tmp)
{
int lnum=cnt[k1];
int rnum=num-lnum-1;
int id=l+lnum;
thi=new node();
thi->v=a[id];
thi->left=build(l,id-1,lnum);
thi->right=build(id+1,r,rnum);
}
else {
int lnum=num-1-cnt[k1-1];
int rnum=cnt[k1-1];
int id=l+lnum;
thi=new node();thi->v=a[id];
thi->left=build(l,id-1,lnum);
thi->right=build(id+1,r,rnum);
}
return thi;
}
int main()
{
//freopen("in.txt","r",stdin);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+1+n);
int l=1;int r=n;
node *head=build(l,r,n);
queue< node* >q;
while(!q.empty())q.pop();
q.push(head);
vector<int>ans;ans.clear();
node *now;
while(!q.empty())
{
now=q.front();q.pop();
ans.push_back(now->v);
if(now->left!=NULL)q.push(now->left);
if(now->right!=NULL)q.push(now->right);
}
for(int i=0;i<ans.size();i++)
{
cout<<ans[i];if(i!=ans.size()-1)cout<<" ";
}
cout<<endl;
return 0;
}
pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)的更多相关文章
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
- PAT甲级——A1064 Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 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 ...
- 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)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 1064 Complete Binary Search Tree[二叉树][难]
1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...
- 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 ...
随机推荐
- PyCryptodome安装使用方法
PyCryptodome是PyCrypto的一个分支.基于PyCrypto2.6.1,多了以下特性: Authenticated encryption modes (GCM, CCM, EAX, SI ...
- sql注入测试(4)--如何防止该类缺陷发生
检查用户输入的合法性,确信输入的内容只包含合法的数据,数据检查应当在客户端和服务器端都执行之所以要执行服务器端验证,是为了弥补客户端验证机制脆弱的安全性.在客户端,攻击者完全有可能获得网页的源代码,修 ...
- (三十一)web 开发基础项目
1. 编写index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- WebSocket协议探究(二)
一 复习和目标 1 复习 协议概述: WebSocket内置消息定界并且全双工通信 WebSocket使用HTTP进行协议协商,协商成功使用TCP连接进行传输数据 WebScoket数据格式支持二进制 ...
- Mac下面配置oh-my-ssh
想了想,把微博里的转到这里来比较靠谱 配置oh-my-ssh: 1.git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh ...
- require.js 加载 js 文件 404 处理(配置无效)
main.js 是 配置文件,data-main 是异步加载,如果在main.js未加载完成的时候,使用了require去加载文件,就会导致配置无效 main.js
- element-ui组件,全选树节点,新增数据子节点数据,出现回填问题
案情分析:全选后父节点被选中保存,在这个树节点下新增数据时,就会出现,也被选中,事实上数据是没有被选中,也就意味着权限未被配置,而显示是已经配置了,显然这个是一个bug 1.处理前,直接用下面的方法很 ...
- Spring Boot实现自定义注解
在Spring Boot项目中可以使用AOP实现自定义注解,从而实现统一.侵入性小的自定义功能. 实现自定义注解的过程也比较简单,只需要3步,下面实现一个统一打印日志的自定义注解: 1. 引入AOP依 ...
- HugePages概述--翻译自19C文档
翻译自: https://docs.oracle.com/en/database/oracle/oracle-database/19/unxar/administering-oracle-databa ...
- jdk在window系统中的配置
其实配置很简单,百度上很多配置的复杂化了,今天办公室的某小白百度了半天也没有配置好. 我使用的是Linux ,就很多简单了很多编译器都是集成的,尤其是现在kali linux 系统觉得做得越来也好 ...