1123 Is It a Complete AVL Tree
1123 Is It a Complete AVL Tree(30 分)
![]() |
![]() |
![]() |
![]() |
Input Specification:
Output Specification:
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
struct Node{
int val;
int height;//以该结点为根结点的子树高度
Node *lchild,*rchild;
Node(),lchild(nullptr),rchild(nullptr){}
};
int n;//结点个数
int getHeight(Node* pNode)
{
;
else return pNode->height;
}
int getBalancedFactor(Node* pNode)
{
return getHeight(pNode->lchild)-getHeight(pNode->rchild);
}
void updateHeight(Node* pNode)
{
pNode->height=max(getHeight(pNode->lchild),getHeight(pNode->rchild))+;
}
void leftRotation(Node* &pNode)
{
Node* temp=pNode->rchild;
pNode->rchild=temp->lchild;
temp->lchild=pNode;
updateHeight(pNode);
updateHeight(temp);
pNode=temp;
}
void rightRotation(Node* &pNode)
{
Node* temp=pNode->lchild;
pNode->lchild=temp->rchild;
temp->rchild=pNode;
updateHeight(pNode);
updateHeight(temp);
pNode=temp;
}
void insert(Node* &root,int val)
{
if(root==nullptr){
root=new Node(val);
return;
}
if(val < root->val){
insert(root->lchild,val);
updateHeight(root);
){
){//LL型
rightRotation(root);
}){//LR型
leftRotation(root->lchild);
rightRotation(root);
}
}
}else{
insert(root->rchild,val);
updateHeight(root);
){
){//RR型
leftRotation(root);
}){//RL型
rightRotation(root->rchild);
leftRotation(root);
}
}
}
}
//层序遍历,并判断是否为完全二叉树
bool levelOrderTraversal(Node* root)
{
;
bool flag=true;
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node* temp=q.front();
q.pop();
if(temp){
printf("%d",temp->val);
cnt++;
if(cnt<n) printf(" ");
q.push(temp->lchild);
q.push(temp->rchild);
}else{
if(cnt<n) flag=false;
}
}
return flag;
}
int main()
{
int val;
Node* root=nullptr;
scanf("%d",&n);
;i<n;i++){
scanf("%d",&val);
insert(root,val);
}
bool flag=levelOrderTraversal(root);
printf("\n%s",flag?"YES":"NO");
;
}
1123 Is It a Complete AVL Tree的更多相关文章
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- 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 ...
- 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树的判断)
嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...
- PAT 1123 Is It a Complete AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 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甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT 1123. Is It a Complete AVL Tree (30)
AVL树的插入,旋转. #include<map> #include<set> #include<ctime> #include<cmath> #inc ...
随机推荐
- 【python常见面试题】之python 中对list去重的多种方法
在python相关职位的面试过程中,会对列表list的去重进行考察.(注意有时会要求保证去重的顺序性) 1.直观方法 li=[1,2,3,4,5,1,2,3] new_li=[] for i in l ...
- resizable可调整尺寸组件
Resizable 可调整尺寸不依赖于其他组件 1.用法:通过标记创建可调整尺寸(resizable)对象 <div class="easyui-resizable" sty ...
- C++面向对象高级编程(四)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一.Static 二.模板类和模板函数 三.namespace 一.Static 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...
- 辛星笔记——VIM学习篇(推荐阅读)
转载自:辛星和您一起学vim脚本第一节 如本文侵犯了您的版权,请联系windeal12@qq.com 这几天在网上看了辛星的一些vim教程博文,觉得很有收获,也很实用,适合入门,所以转载其中一篇留个网 ...
- java.io.IOException: Unable to establish loopback connection
1.错误描述 Starting preview server on port 8080 Modules: HTML5 (/HTML5) 2017-06-17 11:13:04.823:INFO::ma ...
- 细说并发4:Java 阻塞队列源码分析(上)
上篇文章 趣谈并发3:线程池的使用与执行流程 中我们了解到,线程池中需要使用阻塞队列来保存待执行的任务.这篇文章我们来详细了解下 Java 中的阻塞队列究竟是什么. 读完你将了解: 什么是阻塞队列 七 ...
- memcached asp.net
下载文件 memcached 1.解压缩文件到e:\memcached 2.命令行输入 e:\memcached\memcached.exe -d install' 3.命令行输入 e:\memcac ...
- iOS开发错误汇总
人非圣贤孰能无过 dyld: Library not loaded: /... 过而能改善莫大焉 iOS下dyld: Library not loaded: 错误信息解决方案
- scrollTop兼容处理
使用jQuery2.0以下版本的scrollTop()函数来设置当然兼容性当然很好,但有时需要为滚动设置滑动效果.比如,使用animate函数,这里需要做些兼容性处理: 实例:http://sandb ...
- IOI2002 POJ1054 The Troublesome Frog 讨厌的青蛙 (离散化+剪枝)
Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a we ...

.jpg)
.jpg)
.jpg)