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 ...
随机推荐
- powershell -enc参数无法解码base64编码payload的解决方案
powershell的-enc参数允许传入一个base64编码过的powershell脚本字符串作为参数来执行该powershell脚本,该方法常被用于绕过杀毒软件的主动防御机制. 今天下午在做一个后 ...
- TF随笔-11
#!/usr/bin/env python2 # -*- coding: utf-8 -*- import tensorflow as tf my_var=tf.Variable(0.) step=t ...
- 跟着小程学微服务-Mock自动化系统的原理及实现
一.前言 在之前的文章 http://blog.csdn.net/u013970991/article/details/54862772 中已经介绍了"自动化Mock系统0.9版本" ...
- 深度学习(六十四)Faster R-CNN物体检测
- Windows下使用CMake进阶
目录 回顾代码工程中有什么 将需要的东西在cmake脚本CMakeLists.txt中申明 一键型编译 使用nmake 使用msbuild 实现一键编译 参考 在CMake入门实践一文中,我们初略的介 ...
- 用pip安装python 模块OpenSSL
windows下 1.配置好pip命令 下载安装 pip‑1.5.6.win‑amd64‑py2.7.exeor pip‑1.5.6.win32‑py2.7.exe 装好在C:\Python27\Sc ...
- Unity是什么?
Unity是patterns & practices团队开发的一个轻量级.可扩展的依赖注入容器,具有如下的特性: 1. 它提供了创建(或者装配)对象实例的机制,而这些对象实例可能还包含了其它被 ...
- cocos studio pageview看不到indicator指示点
发现如果在cocos studio里操作给PageView创建页面元素时,即使setIndicatorEnabled为true也看到下方的指示点,必须调用addWidgetToPage或者insert ...
- Dapper.Contrib 开发.net core程序,兼容多种数据库
Dapper.Contrib 开发.net core程序,兼容多种数据库 https://www.cnblogs.com/wuhuacong/p/9952900.html 使用Dapper.Contr ...
- 《selenium2 python 自动化测试实战》(5)——键盘事件
键盘事件,就是键盘上的一些操作,比如Ctrl +C,Ctrl+V,Ctrl+X等. 对键盘的操作需要导入另一个键盘的库: from selenium.webdriver.common.keys imp ...

.jpg)
.jpg)
.jpg)