PAT甲级1123. Is It a Complete AVL Tree
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的更多相关文章
- 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【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- vue引入elementUI 报错
在main.js里面引入import 'element-ui/lib/theme-default/index.css'中报错,无法启动项目,这是把package.json里面的webpack改成 1 ...
- Python基础:内置常量
本文根据Python 3.6.5的官文Built-in Constants编写,官文比较短,大家可以直接看原文. 有一些存在于 内置名称空间(the built-in namespace) 的常量,如 ...
- python网络编程-动态导入和断言
一:动态导入importlib 在程序运行的过程中,根据变量或者配置动态的决定导入哪个模块,可以使用模块importlib importlib使用示例 二:断言assert 如果接下来的程序依赖于前面 ...
- 【鬼脸原创】谷歌扩展--知乎V2.0
目的: 用键盘替代鼠标,做一个安静刷知乎的美男(女)子! 功能: 功能 按键 说明 直接定位到搜索框 q 打开 首页 w 打开 话题 e 打开 发现 r 打开 消息 m 打开 ...
- Linux学习笔记:mkdir创建文件夹
文件夹,即目录,在linux中使用mkdir创建. 语法:mkdir dir_name 通过 mkdir 命令可以实现在指定位置创建以 dir_name(指定的文件名)命名的文件夹或目录.要创建文件夹 ...
- Elastic-Job开发指南
开发指南 代码开发 作业类型 目前提供3种作业类型,分别是Simple,DataFlow和Script. DataFlow类型用于处理数据流,它又提供2种作业类型,分别是ThroughputDataF ...
- tidb 记录文档
ansible-playbook stop.yml / start.yml 重启集群,在ansible目录下执行 SHOW STATS_META; 查看统计信息 重启集群:ansible-play ...
- 使用linux mysql客户端建立表时遇到格式解析的问题
发现在notepad++写好的建表脚本,粘贴到linux客户端后,执行时总是报我的脚本有问题. 我看了又看,发现建表脚本本身是没有问题,问题出在"Tab"键上和注释上边了. 解决办 ...
- linux下更换pip源
pip不更换源的话,速度可能非常慢.这里将pip源更换为阿里云源. 1.修改文件~/.pip/pip.conf(没有该文件则创建一个) $ sudo vim ~/.pip/pip.conf 2.写入以 ...
- ubuntu 14.04 Bob 安装
1. 附件依赖项安装$ sudo add-apt-repository ppa:biometrics/bob $ sudo apt-get update $ sudo apt-get install ...