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. JMX RMI 攻击利用

    攻击者通过构造恶意的MBean,调用 getMBeansFromURL 从远程服务器获取 MBean,通过MLet标签提供恶意的MBean对象下载. 前提条件: 允许远程访问,没有开启认证 (com. ...

  2. jenkins发版脚本更新

    jenkins 项目名中明确了 是jar tar.gz war包研发需要提供  项目名-地点-环境(研发.测试.生产)-应用项目名称(admin)-包格式(jar\war\gz) deployment ...

  3. Jenkins与Github集成

    Jenkins目前是手动进行项目构建的,如何才能做到Github并持续集成呢? 配置前要求: 1.Jenkins已经安装Github插件 2.Jenkins服务器已经拥有一个公网IP地址 第一步:配置 ...

  4. 解决IDEA因分配内存而引起的卡顿

    解决IDEA分配内存不足引起卡顿的问题 在使用IDEA的过程中,经常会运行一段时间后程序卡顿.一段代码可能要敲很久或者出现死机状态,严重影响代码书写速度.经查阅资料,IDEA的自动分配内存最大只有75 ...

  5. 在ASP.NET Core上实施每个租户策略的数据库

    在ASP.NET Core上实施每个租户策略的数据库 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: http://g ...

  6. P2014 选课

    题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...

  7. Tomcat端口被占用解决方案

    Tomcat端口被占用解决方法 1.在dos下,输入 netstat -ano|findstr 8080 //说明:查看占用8080端口的进程,显示占用端口的进程 2.taskkill /pid 19 ...

  8. mfc CFileDialog类

    知识点: CFileDialog类 SetBitmap LoadImage 动态显示图片 一.CFileDialog类 构造函数 CFileDialog( BOOL bOpenFileDialog, ...

  9. 【转载】VS配置路径和宏

    原文路径:http://blog.csdn.net/puttytree/article/details/7838419 在介绍项目配置之前,先说明两点 1. 项目配置中,最好不要使用相对路径,更不要使 ...

  10. libgdx学习记录16——资源加载器AssetManager

    AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...