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 ...
随机推荐
- C++(笔记)浅析vector容器的实例
http://www.runoob.com/w3cnote/cpp-vector-container-analysis.html 转载请注明出处 浅然的专栏 https://blog.csdn.net ...
- 转:SQL Server服务器名称与默认实例名不一致的修复方法
--原因分析: --SERVERPROPERTY 函数的 ServerName 属性与@@SERVERNAME 返回相似的信息. --ServerName 属性提供Windows 服务器和实例名称,两 ...
- linux安装vmware出现Gtk-Message: Failed to load module pk-gtk-module canberra-gtk-module的解决方法
使用此方法正确解决了问题, 文章地址如下: http://ian.wang/129.htm 为了防止以后看不到,特截图如下,希望博主不要怪罪.
- 在.NET Core中连接使用Zookeeper
一开始找到的是ZookeeperNetEx,但是很多API都很原始,不怎么好用. 最后确定用Rabbit.Zookeeper来做,他对ZookeeperNetEx进行了封装,要简单不少. 和c语言和j ...
- 目标跟踪算法meanshift优缺点
原博主:http://blog.csdn.net/carson2005/article/details/7341051 meanShift算法用于视频目标跟踪时,采用目标的颜色直方图作为搜索特征,通过 ...
- EasyDSS流媒体服务器Linux emerg getpwnam("xxx") failed解决办法
本文转自EasyDarwin开源团队Alex的博客:http://blog.csdn.net/cai6811376/article/details/73770943 EasyDSS 流媒体服务器是什么 ...
- 快速生成树RTSP
Note: 数据分组:
- 【剑指offer】扑克牌的顺子,C++实现
# 题目 # 思路 顺子满足的条件: 数组长度必须为5 除0外没有重复的牌(0表示大小王) 顺子中最大值和最小值的差值小于5 # 代码 #include <iostream> #inclu ...
- Python递归输出字典所有不同深度的路径
应用场景 假设有这样一个字典结构test_dict = {'a':{'b':{'c':1}},'d':2},test_dict其实可以看作是一种树状结构,其中每个叶子节点深度不一定相同,如果我们希望输 ...
- hihoCoder1599 bfs
特殊的剪枝,整体上和辗转相除法有点像 #1599 : 逃离迷宫4 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi被坏女巫抓进一座由无限多个格子组成的矩阵迷宫. 小 ...

.jpg)
.jpg)
.jpg)