PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/6806292.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~~
给一个序列,对其进行AVL树的插入操作
然后输出该二叉树的层次遍历序列
若该二叉树为满二叉树,则输出YES,否则输出NO。
AVL树的插入操作,模板题,不多说了。
可以在BFS的同时,判断其是否为满二叉树。
一层层遍历,每遍历一个节点,cnt++,统计节点个数。
当第一次遇到-1的时候,若cnt=n,说明已经遍历完n个节点,为满二叉树,输出YES。
否则,说明后面还有节点,两个节点之间有空缺,不符合满二叉树的性质,输出NO。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=;
int n;
struct Node{
int l,r;
int val;
int h;
}; struct AVLTree{
Node node[maxn];
int cnt=;
int height(int u){
if(u==-)
return ;
return node[u].h;
}
/**
k1 is the current root,(向)右旋,顺时针旋转
对k1的左儿子L的左子树L进行了一次插入,所以是LL
*/
int RotateLL(int k1){
int k2;
k2=node[k1].l;
node[k1].l=node[k2].r;
node[k2].r=k1;
node[k1].h=max(height(node[k1].l),height(node[k1].r))+;
node[k2].h=max(height(node[k2].l),node[k1].h)+;
return k2; //new root
}
/**
k1 is the current root,(向)左旋,逆时针旋转
对k1的右儿子R的右子树R进行了一次插入,所以是RR
*/
int RotateRR(int k1){
int k2;
k2=node[k1].r;
node[k1].r=node[k2].l;
node[k2].l=k1;
node[k1].h=max(height(node[k1].l),height(node[k1].r))+;
node[k2].h=max(height(node[k2].r),node[k1].h)+;
return k2;// new root
}
/**
对k1的左儿子L的右子树R进行插入,所以是LR
先对k1的左儿子进行(向)左旋操作
再对k1进行(向)右旋操作
*/
int RotateLR(int k1){
node[k1].l=RotateRR(node[k1].l);
int root=RotateLL(k1);
return root;
}
/**
对k1的右儿子R的左子树L进行插入,所以是RL
先对k1的右儿子进行(向)右旋操作
再对k1进行(向)左旋操作
*/
int RotateRL(int k1){
node[k1].r=RotateLL(node[k1].r);
int root=RotateRR(k1);
return root;
}
/**
插入操作
就分LL\LR\RR\RL四种情况
*/
int insert_val(int val,int root){
//int res=root;
if(root==-){
node[cnt].l=node[cnt].r=-;
node[cnt].val=val;
node[cnt].h=;
root=cnt;
cnt++;
//return cnt;
}
else if(val<node[root].val){
node[root].l=insert_val(val,node[root].l);
int left=node[root].l;
int right=node[root].r;
if(height(left)-height(right)==){
if(val<node[left].val){
root=RotateLL(root);
}
else{
root=RotateLR(root);
}
}
}
else if(val>node[root].val){
node[root].r=insert_val(val,node[root].r);
int left=node[root].l;
int right=node[root].r;
if(height(left)-height(right)==-){
if(val>node[right].val){
root=RotateRR(root);
}
else{
root=RotateRL(root);
}
}
}
else{
//nothing
}
node[root].h=max(height(node[root].l),height(node[root].r))+;
return root;
}
}avltree; bool bfs(int root){
int u;
int cnt=;
bool first=true;
bool mark=true; //标记第一个-1的出现,即没有节点
queue<int>q;
q.push(root);
while(!q.empty()){
u=q.front();
q.pop();
if(u==-){
//按照层次遍历,如果第一次遍历到-1的时候,节点个数cnt=n,则为满二叉树
if(mark && cnt==n)
return true;
else{
mark=false;
continue;
}
}
else{
if(first){
printf("%d",avltree.node[u].val);
first=false;
}
else
printf(" %d",avltree.node[u].val);
cnt++;
q.push(avltree.node[u].l);
q.push(avltree.node[u].r);
}
}
return false;
} int main()
{
int root=-;
int a;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a);
root=avltree.insert_val(a,root);
} if(bfs(root)){
printf("\nYES\n");
}
else{
printf("\nNO\n");
}
return ;
}
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树的更多相关文章
- PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]
题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 1123. Is It a Complete AVL Tree (30)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- PAT甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级题解-1047. Student List for Course (25)-排序
一开始是建立了course[2501][40001]数组,存储每节课的学生编号然后for循环两层输出,但这样复杂度为O(2500*40000),也很明显导致最后时间超时后来发现最多40000学生,每个 ...
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
随机推荐
- Unity Chan 2D Asset
Unity Chan 2D Asset 4月份時,UNITY CHAN 官方網站推出了3D大島こはく,之後也有更新1.11版,而在六月12日時,則釋出了2D版本素材,一樣可以在UNITY CHAN 官 ...
- fedora 28 重新生成 /boot/grub2/grub.cfg
使用情景: 之前电脑安装了windows 7/ fedora 28 双系统,由于特殊原因,需要删除 windows 系统.在格式化硬盘后,我们还需要跟新 grub2 的启动条目:删除grub 启动的界 ...
- Nginx Linux安装与部署
Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行. ...
- python UDP套接字通信
UDPserver.py import socket #导入套接字模块 s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # - socket.A ...
- 使用sstream来进行类型转换
在某种情况下,我们不得不进行整型等数据类型与字符串类型的转换,比如,将“1234”转换为整数,常规的我们可以使用atoi函数来进行转换,或者是写一个循环来做转换,我们在这里也可以使用sstream类来 ...
- VS Code 快捷键使用小技巧
相关文档 官方文档(英文版):Documentation for Visual Studio Code 中文文档(未完成):GitHub - jeasonstudio/CN-VScode-Docs: ...
- 【NOIP2017D2T3】列队
Description Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵.Sylvia所在的方阵中有n × m名学生,方阵的行数为 n, ...
- WPFの获取任意元素的位置
如果布局在Grid中: 方法一: //_stackPanel为子元素,_grid为父元素 Point point = _stackPanel.TranslatePoint(new Point(0, 0 ...
- 最近公共祖先(LCA)模板
第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每行包含两个正整数x.y,表示x结点和y结点之间有一条直接连接的边(数据保证可以构成树). 接下来M行 ...
- nginx做负载均衡和tomcat简单集群
Nginx做负载均衡和TOMCAT简单集群 1.下载安装nginx及其依赖包 ...