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的更多相关文章

  1. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  2. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

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

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

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

  6. pat(A) 1066. Root of AVL Tree

    代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> # ...

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

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

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

随机推荐

  1. 修改Lotus notes默认浏览器

    notes收到的邮件里面的链接,点了以后会自动在notes里打开,但notes很弱,加载页面总是有问题.怎么才能让他默认用ie浏览器打开呢,怎么设置,请教达人. 右下角有个office->Edi ...

  2. unlimited 控制

  3. 数据同步canal客户端

    1.增量订阅.消费设计 get/ack/rollback协议介绍: ① Message getWithoutAck(int batchSize),允许指定batchSize,一次可以获取多条,每次返回 ...

  4. WorldWind源码剖析系列:四叉树瓦片类QuadTile

    四叉树瓦片类QuadTile提供了对影像和地形数据的四叉树访问模型.该类的类图如下. 四叉树瓦片类QuadTile提供的主要字段.属性和方法简要描述如下: public QuadTileSet Qua ...

  5. calico 网络流量 过程 分析 apt-get install telnet

    1.calico node 容器在kubernetes中以DaemonSet 的方式运行,容器的网络模式为hostNetwor,与host共享网络栈,拥有相同的Ip和hostname 2.查看某个po ...

  6. 让win7变成无线路由(需要用管理员权限打开)最后完善.rar

    让win7变成无线路由(需要用管理员权限打开)最后完善.bat @ECHO OFF CLS color 0a netsh wlan show drivers ECHO.★★★★★★★★★★★★★★★★ ...

  7. -bash: start-all.sh: 未找到命令

    解决方案:以root权限进入,找到hadoop安装的目录,进入sbin目录下 输入命令#start-all.sh 出现错误:-bash: start-all.sh: 未找到命令 百度了一下:原来需要输 ...

  8. 【本地服务器】windows下nginx安装操作教程

    1.下载nginx 下载地址:    (可选择下载 Stable version 版本) 2.把安装放到C盘或其他盘的根目录,并解压文件压缩包,可以重命名解压的文件夹,方便找到路径 (注意不要直接双击 ...

  9. R 语言-基础

    R语言 1997年成为GNU项目 开源免费 R官方网址 www.r-project.org R是数据分析领域的语言小巧灵活,通过扩展包来增强功能绘图功能代码简单 开发环境R + RStudio 1.数 ...

  10. python_分布式进程中遇到的问题

    看文档学习分布式进程中遇到了一下问题,文档里面例题是python2.X,我用的python3.x,就出现了一下莫名奇妙的问题,最终版代码先呈上: taskManager.py # coding:utf ...