博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
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树+满二叉树的更多相关文章

  1. 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 ...

  2. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  3. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

  4. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  5. 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 ...

  6. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  7. PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT甲级题解-1047. Student List for Course (25)-排序

    一开始是建立了course[2501][40001]数组,存储每节课的学生编号然后for循环两层输出,但这样复杂度为O(2500*40000),也很明显导致最后时间超时后来发现最多40000学生,每个 ...

  9. PAT甲级题解-1057. Stack (30)-树状数组

    不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...

随机推荐

  1. My strength (C-A-R)

    My strength: I am good at problem resolving Challenge In the first year when I come to America I pas ...

  2. 【PAT】B1050 螺旋矩阵(25 分)

    实在不觉得递归等方式有什么简单的地方,没错我就是用的最笨的方法模拟. 和我一样的小白看代码应该很容易理解. #include<stdio.h> #include<math.h> ...

  3. Mysql基础之 binary关键字

    where子句的字符串比较是不区分大小写的,但是可以使用binary关键字设定where子句区分大小写

  4. 手机上的m3u8视频(缓存)怎么转成MP4?

    一.下载M3u8合并APK,自定义扫描手机中的m3u8文件目录.选择导出的目录,可以多个同时进行m3u8的合并任务. 合并后的文件可以完整播放,但是视频时间只有前十来秒,进度无法拖动. 二.将合并好的 ...

  5. 1024. Video Stitching

    //使用java dfs public int videoStitching(int[][] clips, int T) { //bfs Queue<Integer> queue = ne ...

  6. Jquery 中 ajaxSubmit使用讲解

    最近在使用ajaxForm,随便把使用方法记下下来,以便以后回顾. 1 ,引入依赖脚本 <script type="text/javascript" src="/j ...

  7. C#的深拷贝和浅拷贝

    也许会有人这样解释C# 中浅拷贝与深拷贝区别: 浅拷贝是对引用类型拷贝地址,对值类型直接进行拷贝. 不能说它完全错误,但至少还不够严谨.比如:string 类型咋说? 其实,我们可以通过实践来寻找答案 ...

  8. linux的压缩解压命令全解

    .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)——————————————— .zip解压:un ...

  9. 解惑 ["1", "2", "3"].map(parseInt) 为何返回[1,NaN,NaN]

    javascript中的parseInt与map函数都是常用的函数,可是 ["1", "2", "3"].map(parseInt) 为何返 ...

  10. CSS3系列教程:HSL 和HSL

    使用CSS3 HSL声明同样是用来设置颜色的.下一个呢? HSLA? 是的,这个和RGBA的效果是一样的. HSL声明使用色调Hue(H).饱和度Saturation(s)和亮度Lightness(L ...