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 ...
随机推荐
- linux 的常用命令---------第十阶段
虚拟机三种网络模式 相同模式下的各个虚拟机之间都可以通信----两台虚拟机若都是 nat模式 或 桥接模式 或 仅主机模式,则这两台虚拟机之间是可以通信的. 桥接模式: (配置桥接模式的虚拟机可作为独 ...
- IDEA导包(以junit为例)
## IDEA导包(以junit为例) 1. 准备junit的jar包: * hamcrest-core-1.3.jar * junit-4.12.jar 2. 在项目中新建文件夹:lib 3. 将j ...
- loadrunner中pacing设置01
之前一直也用pacing值来调节TPS,一直觉得它和think time没啥区别.这次项目中,和同事就此展开了讨论,细细一研究发现pacing值门道还是很多的. 如下面三个图: 上图是pacing的三 ...
- Found more than one concrete type for given DbContext Type (xxx.xxxx.xxx) define MultiTenancySideAttribute with Tenant
错误提示: Found more than one concrete type for given DbContext Type (Abp.Zero.EntityFramework.AbpZeroCo ...
- 报错Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
解决方法:import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'输入1:显示所有信息 2:只显示warning和erro ...
- input:file onchange事件无法读取解决方法
最近做项目,移动端的多文件上传,使用input:file读取文件 <input type='file' name='file' multiple accept='image/*' capture ...
- Linux服务-openssh
目录 1. 使用 SSH 访问远程命令行 1.1 OpenSSH 简介 1.2 SSH 版本 1.3 SSH 认证方式 1.4 openSSH 的工作模式 1.5 Secure Shell 示例 1. ...
- c# HttpWebRequest Cookie 设置到 webBrowser 控件
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static exte ...
- 2017-2018-2 20155224『网络对抗技术』Exp8:Web基础
实践具体要求 Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. Web前端javascipt(0.5分) 理 ...
- 20155311《网络对抗》MSF基础应用
20155311<网络对抗>MSF基础应用 实验过程 实验系统 靶机1:Windows XP Professional SP2 ,IP地址:192.168.136.129 靶机2:Wind ...