PAT 甲级 1066 Root of AVL Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then Ndistinct integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the root of the resulting AVL tree in one line.
Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
代码:
#include <bits/stdc++.h>
using namespace std;
struct node {
int val;
struct node *left, *right;
};
node *rotateLeft(node *root) {
node *t = root -> right;
root -> right = t -> left;
t -> left = root;
return t;
} node *rotateRight(node *root) {
node *t = root -> left;
root -> left = t -> right;
t -> right = root;
return t;
} node *rotateLeftRight(node *root) {
root -> left = rotateLeft(root -> left);
return rotateRight(root);
} node *rotateRightLeft(node *root) {
root -> right = rotateRight(root -> right);
return rotateLeft(root);
} int getHeight(node *root) {
if(root == NULL) return 0;
return max(getHeight(root -> left), getHeight(root -> right)) + 1;
} node *insert(node *root, int val) {
if(root == NULL) {
root = new node();
root -> val = val;
root -> left = root -> right = NULL;
} else if(val < root -> val) {
root -> left = insert(root -> left, val);
if(getHeight(root -> left) - getHeight(root -> right) == 2)
root = val < root -> left -> val ? rotateRight(root) : rotateLeftRight(root);
} else {
root -> right = insert(root -> right, val);
if(getHeight(root -> left) - getHeight(root -> right) == -2)
root = val > root -> right -> val ? rotateLeft(root) : rotateRightLeft(root);
}
return root;
} int main() {
int n, val;
scanf("%d", &n);
node *root = NULL;
for(int i = 0; i < n; i ++) {
scanf("%d", &val);
root = insert(root, val);
}
printf("%d", root -> val);
return 0;
}
AVL 树的模板题 第一次写 AVL 树 还不是很会 还有那么多题没写 不会的越来越多?烦
PAT 甲级 1066 Root of AVL Tree的更多相关文章
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 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甲级——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 ...
随机推荐
- etcd管理
操作etcd有命令行工具etcdctl,有两个api版本互不兼容的,系统默认的v2版本,kubernetes集群使用的是v3版本,v2版本下是看不到v3版本的数据的,我也是找了些资料才了解这个情况. ...
- Python2.7-io
io 模块,用于处理流数据,在 python2 下,是内置 file 对象的另一种选择,在 python3 下,此模块是默认的文件和流数据的接口. 1.模块继承关系 IOBase--RawIOBase ...
- java中extends和implements的区别
implements:接口 1.实现一个接口就是要实现该接口中的所有方法(抽象类除外) 2)接口中的方法都是抽象的 多个无关的类可以实现同一个接口,一个类可以实现多个无关的接口 extends:继承父 ...
- 开启路由转发 - route add -net 0.0.0.0 netmask 0.0.0.0 gateway 192.168.0.131 window tracert 追踪路由
1.登录方式内网访问172.28.101.0/19网段的方法:在192.168.1.0/24网段的上网机器上,或在自己的操作机上加个192.168.1.0网段的ip,注意不要跟别人设置的冲突了,并添加 ...
- 微信小程序开发 [01] 小程序基本结构和官方IDE简介
1.小程序账户注册 实际上在进行开发时没有注册小程序账户也是可以的,官方提供的IDE提供实时编译模拟预览,和当前你有没有绑定小程序账户没有关系. 当然,最终你要正式上线你的小程序的话,肯定还是需要账户 ...
- MFC 如何为控件关联变量
所关联的变量常见有两种,一种就是控件变量,一种就是数字变量. 为控件关联变量的方法也有两种,一种是通过软件工具添加,一种是手动添加代码. 软件工具添加,方便简单,但是根据软件的版本不同,以及不同的空间 ...
- STM32烧录的常用方式
stm32烧录常用的方式一般为ST-LINK(或者J-tag)下载仿真和ISP下载 一.仿真器下载 仿真器分为J-TAG和SWD仿真,SWD仿真只需要4根线(VCC.GND.CLK.DATA)就可以了 ...
- 阿里云Linux系统基线检查优化
1.用户权限配置文件的权限优化 描述:设置用户权限配置文件的权限 操作时建议做好记录或备份 chown root:root /etc/passwd /etc/shadow /etc/group /et ...
- TMS320VC5509驱动LCD1602之奇怪问题和时序图
1. 最近调试自己板子上LCD1602的时候,看下测试的时序图,因为下面的时序图导致LCD1602无法显示,下面的时序图是有问题的,E的上升沿和下降沿的时候,RW需要低电平 对比下淘宝上买的可以显示的 ...
- R绘图 第八篇:绘制饼图(ggplot2)
geom_bar()函数不仅可以绘制条形图,还能绘制饼图,跟绘制条形图的区别是坐标系不同,绘制饼图使用的坐标系polar,并且设置theta="y": coord_polar(th ...