题目https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336

题意:

给定n个树,依次插入一棵AVL树,按照层序遍历输出,最后判断这棵AVL树是不是完全二叉树。

思路:

这道题过段时间还要再来手搓一发。AVL模板要记住。

判断是不是完全二叉树的话只用看,如果有一个节点儿子是空,而他之后又出现了至少有一个儿子的节点的话,就不是完全二叉树。【蛮巧妙的】

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n;
const int maxn = ;
vector<int>level[maxn];
typedef struct AvlNode{
int val;
AvlNode *left;
AvlNode *right;
int height;
}*AvlTree, AvlNode; int Max(AvlTree a, AvlTree b)
{
int x = , y = ;
if(a)x = a->height;
if(b)y = b->height;
if(x > y)return x;
else return y;
} AvlTree singleRotateWithRight(AvlTree T)
{
AvlTree L = T->left;
T->left = L->right;
L->right = T;
T->height = Max(T->left, T->right) + ;
L->height = Max(L->left, L->right) + ;
return L;
} AvlTree singleRotateWithLeft(AvlTree T)
{
AvlTree R = T->right;
T->right = R->left;
R->left = T;
T->height = Max(T->left, T->right) + ;
R->height = Max(R->left, R->right) + ;
return R;
} AvlTree doubleRotateWithLeft(AvlTree T)
{
T->left = singleRotateWithLeft(T->left);
return singleRotateWithRight(T);
} AvlTree doubleRotateWithRight(AvlTree T)
{
T->right = singleRotateWithRight(T->right);
return singleRotateWithLeft(T);
} AvlTree Insert(AvlTree T, int val)
{
if(T == NULL){
T = (AvlNode *)malloc(sizeof(struct AvlNode));
if(T){
T->val = val;
T->left = NULL;
T->right = NULL;
T->height = ;
}
}
else if(val < T->val){
T->left = Insert(T->left, val);
int l = , r = ;
if(T->left){
l = T->left->height;
}
if(T->right){
r = T->right->height;
}
if(l - r == ){
if(val < T->left->val){
T = singleRotateWithRight(T);
}
else{
T = doubleRotateWithLeft(T);
}
}
}
else if(val > T->val){
T->right = Insert(T->right, val);
int l = , r = ;
if(T->left)l = T->left->height;
if(T->right)r = T->right->height;
if(r - l == ){
if(val > T->right->val){
T = singleRotateWithLeft(T);
}
else{
T = doubleRotateWithRight(T);
}
}
}
T->height = Max(T->left, T->right) + ;
return T;
} bool after = false, iscomplete = true;
bool first = false;
void levelOrder(AvlTree T)
{
queue<AvlTree>que;
que.push(T);
while(!que.empty()){
AvlTree now = que.front();que.pop();
if(first)printf(" ");
else first = true;
printf("%d", now->val);
level[now->height].push_back(now->val);
if(now->left){
if(after)iscomplete = false;
que.push(now->left);
}
else{
after = ;
}
if(now->right){
if(after)iscomplete = false;
que.push(now->right);
}
else{
after = ;
}
}
} int main()
{
scanf("%d", &n);
AvlTree Tree = NULL;
for(int i = ; i < n; i++){
int x;
scanf("%d", &x);
Tree = Insert(Tree, x);
}
levelOrder(Tree);
printf("\n");
if(iscomplete)printf("YES\n");
else printf("NO\n");
return ;
}

PAT甲级1123 Is It a Complete AVL Tree【AVL树】的更多相关文章

  1. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  2. 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 ...

  3. PAT甲级——A1123 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 ...

  4. 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 ...

  5. pat甲级1123

    1123 Is It a Complete AVL Tree(30 分) An AVL tree is a self-balancing binary search tree. In an AVL t ...

  6. PAT 1066 Root of AVL Tree[AVL树][难]

    1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...

  7. 04-树5 Root of AVL Tree + AVL树操作集

    平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...

  8. 【PAT 甲级】1151 LCA in a Binary Tree (30 分)

    题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...

  9. PAT 甲级 1043 Is It a Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...

随机推荐

  1. 消除导入MNIST数据集发出的警告信息

    原本导入数据集你仅需这样: # Import MNIST data from tensorflow.examples.tutorials.mnist import input_data mnist = ...

  2. 【转载】Jenkins安装以及邮件配置

    转载:http://www.nnzhp.cn/archives/590 Jenkins介绍 Jenkins是一个java开发的.开源的.非常好用持续集成的工具,它能帮我们实现自动化部署环境.测试.打包 ...

  3. Python中__get__, __getattr__, __getattribute__的区别及延迟初始化

    本节知识点 1.__get__, __getattr__, __getattribute__的区别 2.__getattr__巧妙应用 3.延迟初始化(lazy property) 1.__get__ ...

  4. Django REST Framework API Guide 07

    本节大纲 1.Permissions 2.Throttling Permissions 权限是用来授权或者拒绝用户访问API的不同部分的不同的类的.基础的权限划分 1.IsAuthenticated ...

  5. 🍓vue & react 一些重要但没必要死记硬背的东西

  6. android系统添加预置APP(so库自动释放)

    将APK直接放入系统目录中,会导致APK找不到so文件.正常情况下的安装是使用PackageManager,它会将so文件拷贝到系统读取的so目录(system/lib或system/lib64)下, ...

  7. python 列表 元组 字典 集合

    列表 lst = [i for i in range(10)] 切片 # 把下标小于2的显示出来 print(lst[:2]) # 把10个数有大到小输出 print(lst[::-1]) # 把下标 ...

  8. vmware 14 密钥

    VMware 2017 v14.x 永久许可证激活密钥 FF31K-AHZD1-H8ETZ-8WWEZ-WUUVA CV7T2-6WY5Q-48EWP-ZXY7X-QGUWD 来源链接: http:/ ...

  9. Ubuntu18.04更换官方默认更新源sources.list

    ⒈备份官方默认更新源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak 备份官方更新源文件 ⒉编辑 1.打开 vi /etc/apt/sourc ...

  10. windows配置openssl

    下载openssl并安装,下载地址:http://slproweb.com/products/Win32OpenSSL.html 假设安装路径为C:\"Program Files" ...