OJ:https://www.patest.cn/contests/pat-a-practise/1064

(一)23分(3个case未过)代码

建树的规律是我瞎猜的。首先用样例数据分析。

         

对数据排序后:

0 1 2 3 4 5 6 7 8 9

有10个数据,因为是完全二叉树,底层应该有3个叶子,上层有1+2+4=7个结点。用以下代码计算:

    int up_num=,t=;
while(up_num + t* < n){
t*=;
up_num+=t;
}
int leaves_num=n-up_num;

0 1 2  |  3 4 5 6 7 8 9

将左侧叶子结点分割开,对右侧结点进行分析。

6就是根结点。计算方式是取中间。这个算法中所有的“取中间”都满足以下规律:

奇数个数:直接取中间

偶数个数:取中间靠右

定义以下函数计算:

int get_mid(int a,int b){
if((a+b)%){
return (a+b)/+;
}
return (a+b)/;
}

6就是从3到9取中间而来。之后就可以递归建树了。

6的左叶子是0到5取中间的3。

6的右叶子是7到9取中间的8。

……

23分代码:

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map> #define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 1010
#define MAX (1<<30)-1
#define V vector<int> using namespace std; int a[LEN];
int n; typedef struct Node{
int d;
struct Node* l=NULL;
struct Node* r=NULL;
Node(int d=):d(d){
}
}; int get_mid(int a,int b){
if((a+b)%){
return (a+b)/+;
}
return (a+b)/;
} Node * build_tree(int s,int e) {
if(e<s) return NULL;
if(e==s) return new Node(a[s]);
int t=get_mid(s,e);
Node *node=new Node(a[t]);
node->l=build_tree(s,t-);
node->r=build_tree(t+,e);
return node;
} int main(){
// freopen("1064.txt","r",stdin);
I("%d",&n);
int i;
FF(i,n) I("%d",&a[i]);
sort(a,a+n);
int up_num=,t=;
while(up_num + t* < n){
t*=;
up_num+=t;
}
int leaves_num=n-up_num;
int root_i=get_mid(leaves_num,n-);
Node *root=new Node(a[root_i]);
root->l=build_tree(,root_i-);
root->r=build_tree(root_i+,n-);
queue<Node*> q;
q.push(root);
int cnt=;
while(!q.empty()){
Node* tmp=q.front();
q.pop();
cnt++;
O("%d",tmp->d);
if(cnt<n)
O(" ");
if(tmp->l)
q.push(tmp->l);
if(tmp->r)
q.push(tmp->r);
}
return ;
}

看了大佬的博客之后,才知道自己是多么naive。这题其实很简单,利用BST的性质:中序遍历的序列递增有序,再用dfs(其实深搜的本质就是中序遍历)和一个二叉堆来递归建树。最后都不用队列来模拟层序,直接把二叉堆依次输出就可以了。

AC代码:

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map> #define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 10010
#define MAX 0x06FFFFFF
#define V vector<int> using namespace std; int n;
int a[LEN] ,ans[LEN];
int p=-; //p是中序遍历序列的索引,初始化为-1,因为在操作中首先会被+1而变成0 void dfs(int x){ //x是二叉堆(完全二叉树)的索引,根节点是0,叶子节点满足二倍关系
if(x>=n){ //超出界限
p++; //进行了一轮超出界限的操作,意味着操作序列的更新
return; //记得退出,不然会爆栈。找到目标解或者到了递归边界而退出是dfs的必须操作
}
dfs(x*+); //左子树递归
ans[x]=a[p]; //ans数组就是二叉堆,a是中序遍历序列。
dfs(x*+); //右子树递归
} int main(){
// freopen("D:\\input\\A1064.txt","r",stdin);
I("%d",&n);
int i;
FF(i,n) I("%d",&a[i]);
sort(a,a+n);
dfs();
FF(i,n){
O("%d",ans[i]);
if(i!=n-) O(" ");
}
return ;
}

BST | 1064 完全二叉搜索树的更多相关文章

  1. [leetcode]333. Largest BST Subtree最大二叉搜索树子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. 二叉搜索树(BST)详解

    前言:平衡树的前置知识吧 二叉搜索树的定义: 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于或等于它的根节点的值: (2)若右子树不空,则右子 ...

  3. BST(二叉搜索树)的基本操作

    BST(二叉搜索树) 首先,我们定义树的数据结构如下: public class TreeNode { int val; TreeNode left; TreeNode right; public T ...

  4. [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  5. 二叉搜索树-php实现 插入删除查找等操作

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...

  6. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. 数据结构中很常见的各种树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)

    数据结构中常见的树(BST二叉搜索树.AVL平衡二叉树.RBT红黑树.B-树.B+树.B*树) 二叉排序树.平衡树.红黑树 红黑树----第四篇:一步一图一代码,一定要让你真正彻底明白红黑树 --- ...

  8. bst 二叉搜索树简单实现

    //数组实现二叉树: // 1.下标为零的元素为根节点,没有父节点 // 2.节点i的左儿子是2*i+1:右儿子2*i+2:父节点(i-1)/2: // 3.下标i为奇数则该节点有有兄弟,否则又左兄弟 ...

  9. 在二叉搜索树(BST)中查找第K个大的结点之非递归实现

    一个被广泛使用的面试题: 给定一个二叉搜索树,请找出其中的第K个大的结点. PS:我第一次在面试的时候被问到这个问题而且让我直接在白纸上写的时候,直接蒙圈了,因为没有刷题准备,所以就会有伤害.(面完的 ...

随机推荐

  1. 31,Leetcode下一个排列 - C++ 原地算法

    题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...

  2. Kubernetes管理GPU应用

    目录 简介 GPU驱动 Nvidia-docker Nvidia-device-plugin 在Kubernetes上运行GPU应用 附录 简介 伴随着人工智能技术的发展,机器学习的应用场景越来越广泛 ...

  3. jquery库与其他库(比如prototype)冲突的解决方法

    前端开发很容易会遇到jQuery库与其他库冲突的场景,比如和prototype库冲突. 实际上这种冲突是因为不同的第三方JS库争夺对$标识符的控制权引起的. 解决方法,就是使用jQuery提供的jQu ...

  4. git账户配置

    一.生成github的ssh key ssh-keygen ssh-keygen -t rsa -f ~/.ssh/zzf073_rsa -C zzf073@163.com 二.配置账户公钥 1.查看 ...

  5. 在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)

    在上篇文章介绍了Web Api中使用令牌进行授权的后端实现方法,基于WebApi2和OWIN OAuth实现了获取access token,使用token访问需授权的资源信息.本文将介绍在Web Ap ...

  6. alpine安装sshd/ssh server

    1.下载alpine镜像 1 2 3 4 5 6 7 8 9 10 [root@docker43 ~]# docker pull alpine Using default tag: latest Tr ...

  7. ELK + kafka 分布式日志解决方案

    概述 本文介绍使用ELK(elasticsearch.logstash.kibana) + kafka来搭建一个日志系统.主要演示使用spring aop进行日志收集,然后通过kafka将日志发送给l ...

  8. 基于vue+springboot+docker网站搭建【四】安装nginx

    安装nginx 搜索镜像:systemctl search nginx 下载镜像(注意带上版本):docker pull nginx:1.10 查看自己机器的所有镜像:docker images 启动 ...

  9. 英语cabardine麝香cabardine单词

    麝香(cabardine)是麝科动物林麝Moschus berezovskii Flerov. 马麝M. sifanicusPrzewalski或原麝M.moschiferus L.雄体香囊中的干燥分 ...

  10. sqlalchemy相关操作(ORM)

    环境:python3.7,pycharm,mysql ORM(Object-Relational-Mapper) 对象关系映射(ORM)是一种允许您使用面向对象的范例从数据库查询和操作数据的技术,sq ...