PAT甲级1123. Is It a Complete AVL Tree

题意:

在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性。图1-4说明了旋转规则。

现在给出一系列插入,

您应该输出生成的AVL树的级别遍历序列,并告知它是否是完整的二叉树。

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行包含正整数N(<= 20)。

一行中的所有数字都以空格分隔。

输出规格:

对于每个测试用例,将键逐个插入到初始空的AVL树中。然后首先在一行中打印生成的AVL树的级别遍历序列。

并且行尾没有额外的空间。然后在下一行中,如果树完成,打印“是”,否则打印“否”。

思路:

AVL.上次做过一道关于AVL的要会写AVL旋转的模板就好了。最后输出是否是平衡二叉树就ok。

ac代码:

C++

// pat1123.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set> using namespace std; struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
int height;
TreeNode(int x) : val(x) , left(NULL) , right(NULL) , height(0) {}
}; int count_height(TreeNode*& node)
{
if (!node) return -1;
else return node->height;
} void LL(TreeNode* &root)
{
TreeNode* temp = root->left;
root->left = temp->right;
temp->right = root; root->height = max(count_height(root->right), count_height(root->left)) + 1;
temp->height = max(count_height(temp->right), count_height(temp->left)) + 1; root = temp;
} void RR(TreeNode* &root)
{
TreeNode* temp = root->right;
root->right = temp->left;
temp->left = root; root->height = max(count_height(root->right), count_height(root->left)) + 1;
temp->height = max(count_height(temp->right), count_height(temp->left)) + 1; root = temp;
} void RL(TreeNode* &root)
{
LL(root->right);
RR(root);
} void LR(TreeNode* &root)
{
RR(root->left);
LL(root);
} void insert(int val,TreeNode* &root)
{
if (!root)
{
root = new TreeNode(val);
return;
} if (root->val < val)
{
insert(val, root->right);
if (count_height(root->right) - count_height(root->left) > 1)
{
if (val > root->right->val) RR(root);
else RL(root);
}
}
else
{
insert(val, root->left);
if (count_height(root->left) - count_height(root->right) > 1)
{
if (val > root->left->val) LR(root);
else LL(root);
}
} root->height = max(count_height(root->left), count_height(root->right)) + 1;
} int main()
{
int n,val;
scanf("%d", &n);
TreeNode* root = NULL;
for (int i = 0; i < n; i++)
{
scanf("%d", &val);
insert(val, root);
} //level order traversal
queue<TreeNode*> q;
q.push(root);
vector<TreeNode*> level_order(1,NULL);
while (!q.empty())
{
TreeNode* top = q.front();
q.pop();
level_order.push_back(top);
if (top->left) q.push(top->left);
if (top->right) q.push(top->right);
}
for (int i = 1; i < n; i++)
printf("%d ", level_order[i]->val);
printf("%d\n", level_order[n]->val); //is complete
bool flag = true;
for (int i = 1; i <= n; i++)
{
if (2 * i <= n && (!level_order[i]->left || level_order[i]->left->val != level_order[2 * i]->val))
{
flag = false;
break;
}
else if(2 * i + 1 <= n && (!level_order[i]->right || level_order[i]->right->val != level_order[2 * i + 1]->val))
{
flag = false;
break;
}
}
if (flag) printf("YES\n");
else printf("NO\n"); return 0;
}

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

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

  2. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  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. 1123 Is It a Complete AVL Tree

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

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

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

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

  9. PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. MySQL服务器修改主机名后问题解决

    1.单机MySQL主机名修改 今天无事看到自己的主机名不对,于是改了一下,以便区分服务器,那只重启MySQL时出现下面错误: MySQL manager or server PID file coul ...

  2. 使用Cache缓存

    存放位置:服务器内存,用于频繁访问且不轻易更改的内容缓存. string CacheKey = "CT1"; //检索指定项, object objModel = Cache.Ge ...

  3. 二十、springboot之jpa开发@MappedSuperclass 注解说明

    @MappedSuperclass使用条件: 当我们进行开发项目时,我们经常会用到实体映射到数据库表的操作,此时我们经常会发现在我们需要映射的几个实体类中,有几个共同的属性,例如编号ID,创建者,创建 ...

  4. 排序算法——Shell排序

    二.Shell排序 Shell排序也叫“缩减增量排序”(disminishing increment sort),基于插入排序进行. Shell建议的序列是一种常用但不理想的增量序列:1,...,N/ ...

  5. vue全面介绍--全家桶、项目实例

    简介 “简单却不失优雅,小巧而不乏大匠”. 2016年最火的前端框架当属Vue.js了,很多使用过vue的程序员这样评价它,“vue.js兼具angular.js和react.js的优点,并剔除了它们 ...

  6. MyEclipse文本对比界面样式修改

    MyEclipse刚安装好,使用文件对比的时候,发现两边的对比颜色非常浅,不同的地方不容易发现,可以通过以下配置将显示颜色调深一点. 配置 效果

  7. MongoDB中多表关联查询(

    1.使用aggregate 查看表数据 db.getCollection('reports').aggregate([ { $lookup: { from: "process", ...

  8. MyBatis框架的基本使用

    MyBatis框架简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...

  9. mysql数据库查找类型不匹配

    无意中看到10级学长的博客,提到了mysql数据库类型查找不匹配的问题,博客地址是:卢俊达 . 数据库中建表中会对每个属性进行类型划分,然后在查找数据库select时: MySQL 的文档 (Type ...

  10. Docker网络和存储

    本节内容: Docker网络访问 端口映射 数据管理 一.Docker网络访问 1. docker自带的网络 docker network ls:列出当前docker中已有的网络 docker net ...