PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree
题意:
构造AVL树,返回root点val。
思路:
了解AVL树的基本性质。
AVL树
ac代码:
C++
// pat1066.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>
using namespace std;
struct AvlNode
{
int data;
AvlNode* left;
AvlNode* right;
int height;
AvlNode(int x) : data(x), left(NULL), right(NULL), height(0) {}
};
int height(AvlNode* root)
{
return root == NULL ? -1 : root->height;
}
void LLRotate(AvlNode*& root) //右单
{
AvlNode* temp = root->left;
root->left = temp->right;
temp->right = root;
temp->height = max(height(temp->left), height(temp->right)) + 1;
root->height = max(height(root->left), height(root->right)) + 1;
root = temp;
}
void RRRotate(AvlNode*& root) //左单
{
AvlNode* temp = root->right;
root->right = temp->left;
temp->left = root;
temp->height = max(height(temp->left), height(temp->right)) + 1;
root->height = max(height(root->left), height(root->right)) + 1;
root = temp;
}
void RLRotate(AvlNode*& root)
{
LLRotate(root->right);
RRRotate(root);
}
void LRRotate(AvlNode*& root)
{
RRRotate(root->left);
LLRotate(root);
}
void insert(const int& x, AvlNode*& root)
{
if (!root)
{
root = new AvlNode(x);
}
else if (x < root->data)
{
insert(x, root->left);
if (height(root->left) - height(root->right) == 2)
{
if (x < root->left->data) LLRotate(root);
else LRRotate(root);
}
}
else if (x > root->data)
{
insert(x, root->right);
if (height(root->right) - height(root->left) == 2)
{
if (x > root->right->data) RRRotate(root);
else RLRotate(root);
}
}
root->height = max(height(root->left), height(root->right)) + 1;
}
int main()
{
AvlNode* root = NULL;
int N;
int i;
int num[22];
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%d", &(num[i]));
}
for (int i = 0; i < N; i++)
{
insert(num[i], root);
}
printf("%d", root->data);
return 0;
}
PAT甲级1066. Root of AVL Tree的更多相关文章
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***
1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...
- PAT 甲级 1066 Root of AVL Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...
- PAT甲级——A1066 Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]
题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- pat(A) 1066. Root of AVL Tree
代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> # ...
- PAT甲级:1066 Root of AVL Tree (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- 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, ...
- PTA (Advanced Level) 1066 Root of AVL Tree
Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...
随机推荐
- php中的转义函数
<?php parse_url 解析URL, 返回各组成部分 urlencode/urldecode url编码/解码 htmlentities 将字符串转化为html实体 htmlentiti ...
- spring boot注解学习记
@Component Compent等效于xml文件中的Bean标注,Autowired自动初始化Bean是通过查找Component注解实现的,在增加Component后还是Autowired找不到 ...
- apache 各种配置
//apache 的网站配置文件 /usr/local/apache2/conf/extra/httpd-vhosts.conf -->在编辑这个文件前需要去httpd.conf把这个文件的注释 ...
- SyntaxError: Missing parentheses in call to 'print' 这个错误原因是Python版本问题
问题 print "www.baidu.com" Python2 print ("www.baidu.com") Python3 出 ...
- falsk注册etcd
部署web服务集群时,我们希望能动态调整集群大小.当一个新的节点启动时,可以将自己的信息注册给master, 让master把它加入到集群里, 关闭之后也可以把自己从集群中删除.我这里使用的是flas ...
- PHP--- JSON和数组的转换
一.json_encode() <?php $arr =array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_ ...
- cenos6.5安装vsftp
1.安装 yum install vsftpd 2.配置 vi /etc/vsftpd/vsftpd.conf 检查是否如下配置 anonymous_enable=NO #禁止匿名访问 chroot_ ...
- Linux中如何配置IP相关文件
Linux中如何配置IP 与网络相关的文件:1) /etc/sysconfig/network 设置主机名称及能否启动Network2) /etc/sysconfig/network-script ...
- EasyUi–7.tab和datagrid和iframe的问题
1. 多个tab切换,第2个不显示 动态添加tab Iframe页面的方法 展开 折叠 <script type="text/javascript"> $(functi ...
- MINIBASE源代码阅读笔记之heapfile
Heapfile 用来管理heap file里的dir page们 成员 _firstDirPageId:这个文件的第一个dir page _ftype:文件类型 _file_deleted:删除的时 ...