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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- InfluxDB系列之一安装及简单运维(未完成,需要继续写)
InfluxDB 是一个开源分布式时序.事件和指标数据库.使用 Go 语言编写,无需外部依赖.其设计目标是实现分布式和水平伸缩扩展. 它有三大特性: 1. Time Series (时间序列):你可以 ...
- java基础53 IO流技术(转换流)
1.转换流 1.输入字节的转换流:InputStreamReader是字节流转为字符流的桥梁,可以把输入字节流转换为输入字符流 2.输出字节流的转换流:OutputStreamWriter是字符 ...
- java基础46 IO流技术(输出字符流/缓冲输出字符流)
一.输出字符流 1.1.输出字符流体系 --------| Writer:输出字符流的基类(抽象类) ----------| FileWriter:向文件输出数据输出字符流(把程序中的数据写到硬盘中 ...
- MyBatis框架的基本使用
MyBatis框架简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...
- ZOJ 3962 Seven Segment Display(数位DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目大意: 有t组数据. 给你一个n,和8位的十六进制数s ...
- SQLServer 查看备份进度
SELECT DB_NAME(er.[database_id]) [DatabaseName], er.[command] AS [CommandType], er.[percent_comp ...
- 动态规划面试题基础合集1--数学三角形,LIS , LCS, CSD
动态规划的一般思路是分为四步,即:寻找最优子结构.递归定义最优子结构.自底向上求解最优子结构和构造最优解. 接下来我列举出几个常见的动态规划面试题进行说明. (1)数学三角形:比较简单,直接贴一个我看 ...
- 学习python绘图
学会python画图 # 使用清华的pip源进行安装sklearn # pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U sciki ...
- iOS中URL的解码和转义问题
在iOS开发中,使用NSURLConnection去请求google places api时,如果请求的url中包含中文,则返回的结果为空,URL不能被google识别.NSString *_urlS ...
- N皇后问题的实现
N皇后问题是一个经典的问题,是回溯算法的典型案例.它是由国际西洋棋棋手马克斯·贝瑟尔于1848年提出的八皇后问题延伸而来的,具体要求如下:在N*N的方格棋盘放置N个皇后,使她们彼此不相互攻击,即任意2 ...