题意:

输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
typedef struct node{
int val;
node*left,*right;
};
node*left_rotate(node*root){
node*tamp=root->right;
root->right=tamp->left;
tamp->left=root;
return tamp;
}
node*right_rotate(node*root){
node*tamp=root->left;
root->left=tamp->right;
tamp->right=root;
return tamp;
}
node*left_right(node*root){
root->left=left_rotate(root->left);
return right_rotate(root);
}
node*right_left(node*root){
root->right=right_rotate(root->right);
return left_rotate(root);
}
int get_height(node*root){
if(root==NULL)
return ;
return max(get_height(root->left),get_height(root->right))+;
}
node*inser(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=inser(root->left,val);
if(get_height(root->left)-get_height(root->right)==)
root=val<root->left->val?right_rotate(root):left_right(root);
}
else{
root->right=inser(root->right,val);
if(get_height(root->right)-get_height(root->left)==)
root=val>root->right->val?left_rotate(root):right_left(root);
}
return root;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i)
cin>>a[i];
node*ans=NULL;
for(int i=;i<=n;++i)
ans=inser(ans,a[i]);
cout<<ans->val;
return ;
}

【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)的更多相关文章

  1. 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 sub ...

  2. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

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

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

  5. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  6. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

  7. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  8. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  9. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  10. PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题

    1043 Is It a Binary Search Tree (25 分)   A Binary Search Tree (BST) is recursively defined as a bina ...

随机推荐

  1. 【转载】C++面试题(1-50)

    转自http://www.jobui.com/mianshiti/it/cpp/5017/ 1.    面向对象的程序设计思想是什么? 答:把数据结构和对数据结构进行操作的方法封装形成一个个的对象. ...

  2. hdu 1045 Fire Net(二分图)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意为给定一个最大为4*4的棋盘,棋盘可以放置堡垒,处在同一行或者同一列的堡垒可以相互攻击, ...

  3. php preg正则表达式的组成部分

    定界符号 : 多种都可以,常用为// 原子 : 最小的一个匹配单位 (放在定界符中),在一个正则表达式中,至少要有一个原子 1,打印字符(a-z A-Z 0-9 ~!@#$%^&*()_+.. ...

  4. ubuntu 下的ftp详细配置

    FTP(文件传输协议)是一个较老且最常用的标准网络协议,用于在两台计算机之间通过网络上传/下载文件.然而, FTP 最初的时候并不安全,因为它仅通过用户凭证(用户名和密码)传输数据,没有进行加密. 警 ...

  5. opencv:USM锐化

    USM:unsharp mask 对小的细节干扰小,对大的细节进行锐化 Mat dst; Mat blur_image; GaussianBlur(src, blur_image, Size(3, 3 ...

  6. redis安装与基本使用

    什么是Redis 什么是NoSQL 介绍Redis之前,先了解下NoSQL (Not noly SQL)不仅仅是SQL 属于非关系型数据库:Redis就属于非关系型数据库 传统的Mysql ,orac ...

  7. AcWing 854. Floyd求最短路 多源 邻接矩阵

    //不存在负权回路 //边权可能为负数 #include <cstring> #include <iostream> #include <algorithm> us ...

  8. AcWing 841. 字符串哈希

    //快速判断两次字符串是不是相等 #include<bits/stdc++.h> using namespace std ; typedef unsigned long long ULL; ...

  9. python中字符串内置方法

    字符串类型 作用:定义姓名.性别等 定义方式: s='lzs' #\n换行 \t缩进4个空格 \r回退上一个打印结果,覆盖上一个打印结果 加上一个\让后面的\变得无意义 内置方法: (优先掌握) 1. ...

  10. IEC 60958 && IEC 61937

    IEC 60958 IEC 60958是一种传递数字音频的接口规范,相比I2S,IEC60958通过一根线同时传递时钟信号和数据信号.IEC 60958用来传递两channel,16/20/24bit ...