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:

  1. 5
  2. 88 70 61 96 120

Sample Output 1:

  1. 70

Sample Input 2:

  1. 7
  2. 88 70 61 96 120 90 65

Sample Output 2:

  1. 88
 

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct node {
  4. int val;
  5. struct node *left, *right;
  6. };
  7. node *rotateLeft(node *root) {
  8. node *t = root -> right;
  9. root -> right = t -> left;
  10. t -> left = root;
  11. return t;
  12. }
  13.  
  14. node *rotateRight(node *root) {
  15. node *t = root -> left;
  16. root -> left = t -> right;
  17. t -> right = root;
  18. return t;
  19. }
  20.  
  21. node *rotateLeftRight(node *root) {
  22. root -> left = rotateLeft(root -> left);
  23. return rotateRight(root);
  24. }
  25.  
  26. node *rotateRightLeft(node *root) {
  27. root -> right = rotateRight(root -> right);
  28. return rotateLeft(root);
  29. }
  30.  
  31. int getHeight(node *root) {
  32. if(root == NULL) return 0;
  33. return max(getHeight(root -> left), getHeight(root -> right)) + 1;
  34. }
  35.  
  36. node *insert(node *root, int val) {
  37. if(root == NULL) {
  38. root = new node();
  39. root -> val = val;
  40. root -> left = root -> right = NULL;
  41. } else if(val < root -> val) {
  42. root -> left = insert(root -> left, val);
  43. if(getHeight(root -> left) - getHeight(root -> right) == 2)
  44. root = val < root -> left -> val ? rotateRight(root) : rotateLeftRight(root);
  45. } else {
  46. root -> right = insert(root -> right, val);
  47. if(getHeight(root -> left) - getHeight(root -> right) == -2)
  48. root = val > root -> right -> val ? rotateLeft(root) : rotateRightLeft(root);
  49. }
  50. return root;
  51. }
  52.  
  53. int main() {
  54. int n, val;
  55. scanf("%d", &n);
  56. node *root = NULL;
  57. for(int i = 0; i < n; i ++) {
  58. scanf("%d", &val);
  59. root = insert(root, val);
  60. }
  61. printf("%d", root -> val);
  62. return 0;
  63. }

  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. linux 的常用命令---------第十阶段

    虚拟机三种网络模式 相同模式下的各个虚拟机之间都可以通信----两台虚拟机若都是 nat模式 或 桥接模式 或 仅主机模式,则这两台虚拟机之间是可以通信的. 桥接模式: (配置桥接模式的虚拟机可作为独 ...

  2. IDEA导包(以junit为例)

    ## IDEA导包(以junit为例) 1. 准备junit的jar包: * hamcrest-core-1.3.jar * junit-4.12.jar 2. 在项目中新建文件夹:lib 3. 将j ...

  3. loadrunner中pacing设置01

    之前一直也用pacing值来调节TPS,一直觉得它和think time没啥区别.这次项目中,和同事就此展开了讨论,细细一研究发现pacing值门道还是很多的. 如下面三个图: 上图是pacing的三 ...

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

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

  6. input:file onchange事件无法读取解决方法

    最近做项目,移动端的多文件上传,使用input:file读取文件 <input type='file' name='file' multiple accept='image/*' capture ...

  7. Linux服务-openssh

    目录 1. 使用 SSH 访问远程命令行 1.1 OpenSSH 简介 1.2 SSH 版本 1.3 SSH 认证方式 1.4 openSSH 的工作模式 1.5 Secure Shell 示例 1. ...

  8. c# HttpWebRequest Cookie 设置到 webBrowser 控件

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static exte ...

  9. 2017-2018-2 20155224『网络对抗技术』Exp8:Web基础

    实践具体要求 Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. Web前端javascipt(0.5分) 理 ...

  10. 20155311《网络对抗》MSF基础应用

    20155311<网络对抗>MSF基础应用 实验过程 实验系统 靶机1:Windows XP Professional SP2 ,IP地址:192.168.136.129 靶机2:Wind ...