题目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. 出现Failed to get convolution algorithm的解决方法

    当运行卷积神经时出现了问题:Failed to get convolution algorithm. This is probably because cuDNN failed to initiali ...

  2. java -jar参数携带问题

    方式一 -DpropName=propValue的形式携带,要放在-jar参数前面,亲测,放在它后面好像取不到值 java -fileName=JOURNAL_TREENODE_DATA-201904 ...

  3. 【easy】112.path sum 113.-----------------

    求是否有从根到叶的路径,节点和等于某个值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tree ...

  4. wx获取地理位置

    1.公众号配置. 2.引入js 一个放在根目录下的txt文件. 3.1)第一个ajax为获取后台传给的wx.config需要的参数:wx.ready().通过ready接口处理成功验证.然后才是wx. ...

  5. 微信小程序:将中文语音直接转化成英文语音

    作者:瘟小驹    文章来源<微信小程序个人开发全过程> 准备工作: 准备工具:Eclipse.FileZilla.微信开发者工具.一个配置好SSL证书(https)的有域名的服务器 所需 ...

  6. eclipse 报错问题:java.lang.ClassNotFoundException:

    解决方法:https://www.cnblogs.com/whatlonelytear/articles/5921978.html

  7. vue-cli 打包编译 -webkit-box-orient: vertical 被删除解决办法

    前言 -webkit-box-orient: vertical在本地开发环境运行都没问题,一旦打包以后就会丢失 正文 原因: -webkit-box-orient: vertical  这个属性被 o ...

  8. IE9以及以下不支持jquery ajax跨域问题

    1.代码中加 jQuery.support.cors = true; 2. 设置ie浏览器 工具->Internet 选项->安全->自定义级别” 将“其他”选项中的“通过域访问数据 ...

  9. bzoj1040基环树

    ... st#include<cstdio> #include<iostream> #include<algorithm> #include<cmath> ...

  10. php无限极分类方法

    仅供参考: //控制器 $data = M('category')->select(); $datas = D('Category')->_getTree($data, 0,0,TRUE) ...