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中urllib.urlencode的时候出错:UnicodeEncodeError: ‘ascii’的记录
原文我参见了:http://hi.baidu.com/setcookie/item/ddc6b694b1f13435336eeb3c 为毛我要费事的写下了,就是为了让自己记录一下,下面的也是直接摘录过 ...
- Cassandra cqlsh - connection refused
启动cqlsh时,保存如下: Connection error: ('Unable to connect to any servers', {'127.0.0.1': error(111, " ...
- Linux:tee命令详解
tee tee命令用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin,简单的说就是把数据重定向到给定文件和屏幕上. 语法 tee(选项)(参数) 选项 -a:向文件 ...
- jsp页面图片显示不出来
jsp页面就是MyJsp.jsp <body> This is my JSP page. <br> <img src="img/top.jpg"> ...
- final方法,abstract方法和abstract类,native方法
final方法 1.为了确保某个函数的行为在继承过程中保持不变,并且不能被覆盖(override),可以使用final方法. 2.为了效率上的考虑,将方法声明为final,让编译器对此方法的调用进行优 ...
- vue和react
1. 数据渲染 vue是使用template模板进行渲染,react使用的是jsx语法,对组件进行渲染 vue模板中使用{{ this.data }} 双括号包着变量,代表变量表示的值.外面那层表示需 ...
- 依存可视化︱Dependency Viewer——南京大学自然语言处理研究组
来源网页:http://nlp.nju.edu.cn/tanggc/tools/DependencyViewer.html 视频演示网页:http://nlp.nju.edu.cn/tanggc/to ...
- 构造字典:DictionaryBase类和SortedList类
DictionaryBase 类 msdn对DictionaryBase的文档解释 泛型KeyValuePair类 msdnd对泛型KeyValuePair类的文档解释 SortedList类 RUN ...
- 现在很火的数据科学到底是什么?你对做DATA SCIENTIST感兴趣吗?
转自– Warald (Email: iamxiaoning@gmail.com) 博客: http://www.1point3acres.com,微博:http://www.weibo.com/wa ...
- 拦截器springmvc防止表单重复提交【3】3秒后自动跳回首页【重点明白如何跳转到各自需要的页面没有实现 但是有思路】
[1]定义异常类 [重点]:异常类有个多参数的构造函数public CmsException(String s, String... args),可以用来接受多个参数:如(“异常信息”,“几秒跳转”, ...

.jpg)
.jpg)
.jpg)