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 ...
随机推荐
- LeetCode OJ:Peeking Iterator(peeking 迭代器)
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- OC-初识面向对象
面向对象和面向过程思想 OC是面向对象的,C是面向过程的.面向对象和面向过程只是解决问题的两种不同思想 面向对象和面向过程的区别 以用电脑听歌为例子 面向过程 打开电脑 播放电脑中的歌曲 关闭电脑 面 ...
- Makefile.am文件的实例讲解
Makefile.am是一种比Makefile更高层次的编译规则,可以和configure.in文件一起通过调用automake命令,生成Makefile.in文件,再调用./configure的时候 ...
- tensorflow windows
conda create -n py35 python=3.5 activate py35 pip install --ignore-installed --upgrade https://stora ...
- HDU 1806
http://acm.hdu.edu.cn/showproblem.php?pid=1806 非常玄妙的rmq问题,这个st算法有点神 #include <iostream> #inclu ...
- ubuntu16.04 LTS grafana安装与启动
1.首先从官网上下载相应的包,网址为:http://grafana.org/download 2.安装 cd Downloads sudo dpkg -i grafana_4.1.2-14869897 ...
- BZOJ5435: 取石子(博弈)(占位)
5435: 取石子 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 35 Solved: 10[Submit][Status][Discuss] De ...
- BZOJ1015: [JSOI2008]星球大战starwar【并查集】【傻逼题】
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- MyEclipse异常关闭导致Tomcat不能启动的问题
由于MyEclipse的异常关闭从而导致Tomcat并没有关闭,所以再次启动Tomcat当然是无法启动的啦,解决方法:在任务管理器中关闭一个叫javaw.exe的进程,如果你这时已经启动了MyEcli ...
- 携程 决赛 第一题 Crossword
//真是郁闷的一晚上//比赛时看到这题是就感觉会做,感觉思路清晰 就去准备找第二题 ,因为感觉第二题是个经典问题,(我不会计算几何),就去搜索了下,然后找到求最小面积的,改来改去,一直Wa// 然后就 ...

.jpg)
.jpg)
.jpg)