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 ...
随机推荐
- Java POI单元格使用心得
1: /** * Created by liuguangxin on 2018/5/16. * <p> * MergeRegion:表示excel中cell的信息,startRow与end ...
- 可以获取JVM信息的一些管理工具类
一些可以获取JVM信息的java工具类 BufferPoolMXBean.class ClassLoadingMXBean.class CompilationMXBean.class GarbageC ...
- day2-作业及答案
作业:第一组: 1.接收用户输入一个年份,判断是否是闰年(判断闰年的方法是该年能被4整除并且不能被100整除,或者是可以被400整除) 2.接收用户输入一组整数,输入负数时结束输入,输出这组数字的和: ...
- 分享一个excel根据文件超链接获取链接文档的最后更新时间
#获取制定单元格内超链接对应的链接地址Sub geturi() For Each cell In Range("E3:E43") If cell.Hyperlinks.Count ...
- Android 将拼接好并加上边框的图片保存到内存卡中
通过前两篇文章,问们学会了怎样拼接图片.给拼接好的图片加上边框样式,但这还不够,忙活了大半天 终于拼接好并给图片美化了,但是程序一旦推出,之前做的工作都白费了.这时我们会想,能不能把拼接好的图片保存起 ...
- HUE配置HIVE
HIVE配置 修改hue.ini配置文件 [beeswax] hive_server_host=node1 hive_server_port= hive_conf_dir=/usr/hive-/con ...
- storm报错:Exception in thread "main" java.lang.RuntimeException: InvalidTopologyException(msg:Component: [mybolt] subscribes from non-existent stream: [default] of component [kafka_spout])
问题描述: storm版本:1.2.2,kafka版本:2.11. 在使用storm去消费kafka中的数据时,发生了如下错误. [root@node01 jars]# /opt/storm-1. ...
- 大数据入门第二十二天——spark(二)RDD算子(1)
一.RDD概述 1.什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的 ...
- Luogu P3370 【模板】字符串哈希
方法很多,hash,双hash(个人想到一种三hash),挂链,还有STL: map 乱搞 CODE #include<iostream> #include<map> #inc ...
- python 回溯法 子集树模板 系列 —— 8、图的遍历
问题 一个图: A --> B A --> C B --> C B --> D B --> E C --> A C --> D D --> C E -- ...