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

    htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容.项目可以模拟浏览器运行,被誉为java浏览器的开源实现.是一个没有界面的浏览器,运行速 ...

  2. MP实战系列(十三)之批量修改操作(前后台异步交互)

    MyBatis的批量操作其实同MyBatis基本是一样的.并无多大区别,要说区别,除了封装的方法之外,主要就是注解方面的区别,比如@TableId.@TableField.@TableName等等区别 ...

  3. PAT B1040 有几个PAT (25 分)

    字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位(P),第 4 位(A),第 6 位(T). 现 ...

  4. HDU1754

    https://vjudge.net/contest/66989#problem/B 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜 ...

  5. 基于LBS平台的iOS开发

    LBS,即Location Based Services,基于位置服务,用于定位.导航等功能,比如地图应用.订外卖等的app就需要这个功能. 在这里我使用的是高德LBS开放平台,地址:http://l ...

  6. hiveserver2连接报错: User: root is not allowed to impersonate anonymous (state=08S01,code=0)

    使用HiveServer2运行时,启动好HiveServer后运行 private static String url = "jdbc:hive2://192.168.213.132:100 ...

  7. 文理分科 BZOJ3894 & happiness BZOJ2127

    分析: 最小割(一开始我没看出来...后来经过提点,大致理解...),不选则割的思想. 我们先这样考虑,将和选理相关的和S相连,与选文相关的和T相连,如果没有第二问,那么建图就是简单的S连cnt,cn ...

  8. Oracle-归档日志详解(运行模式、分类)

    一.Oracle日志分类 分三大类: Alert log files--警报日志,Trace files--跟踪日志(用户和进程)和            redo log 重做日志(记录数据库的更改 ...

  9. Django Rest Framework源码剖析(五)-----解析器

    一.简介 解析器顾名思义就是对请求体进行解析.为什么要有解析器?原因很简单,当后台和前端进行交互的时候数据类型不一定都是表单数据或者json,当然也有其他类型的数据格式,比如xml,所以需要解析这类数 ...

  10. 20155204《网络对抗》Exp7 网络欺诈防范

    20155204<网络对抗>Exp7 网络欺诈防范 一.基础问题回答 1.通常在什么场景下容易受到DNS spoof攻击 在不安全的网络环境下访问网站. 2.在日常生活工作中如何防范以上两 ...