题意:

输入一个正整数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. 传奇服务端添加双倍经验卷的方法 双倍经验卷轴DB示例展示

    第一步我们在DBC数据库中添加好双倍经验卷轴DB,以下是现成的双倍经验卷DB,导入到DB里面就可以了. 222;双倍经验卷;31;0;1;20;0;0;265;0;0;0;0;0;0;0;0;0;0; ...

  2. STA之RC Corner拾遗

    Q:还有一种RC corner 带后缀『_T』,只用于setup signoff,T指的是什么? A:T代表tighten,在rc的variation上的sigma分布比不带T的更紧,因此只能用于se ...

  3. P1177排序题解

    这恐怕是一道 坑最多 最经典 的题目了. 这道题有两种解题方法: 1.自己写个排序函数 这里我们用最最最最常用的快速排序: #include <iostream> #define ll l ...

  4. numpy rand函数的应用

    以后使用rand(), randint()等函数. 随机浮点类型数值(均匀分布) numpy.random.rand() 产生[0,1)内的浮点型随机数 numpy.random.rand(value ...

  5. springmvc validator和springContext validator的区别

    1.springContext validator 依赖于代理实现 MethodValidationInterceptor Set<ConstraintViolation<Object&g ...

  6. opencv安装中的各种问题汇总

    问题1:opencv-2.4.10/modules/gpu/src/nvidia/core/NCV.cu(356): error : namespace "std" has no ...

  7. JDBC 基础用法学习

    JDBC概述 java 数据库链接,sun公司退出的 java 访问数据库的标准规范接口 是一种用于执行SQL语句的 java API 可以作为多种关系数据库提供统一接口 是一组 java 工具类和接 ...

  8. iCCID激活终结,苹果iPhone卡贴机“辉煌”时代落幕

       iPhone卡贴机,是一个神奇的存在.所谓的iPhone卡贴机,原本是"有锁机".它们通常是国外运营商的合约机,为了限制使用地域而"上锁",不能直接在国内 ...

  9. JDK动态代理+反射实现动态修改注解属性值

    这是最近朋友的一个需求,正好闲来无聊有些时间,跟着研究一下,如有不正确的地方,欢迎大家指正~ 一.准备自定义注解 注:如何实现自定义注解,请移步百度. 二.实现 1.实现方式1:通过反射+动态代理动态 ...

  10. mui 把内容复制到剪切板

    function copyShareUrl(){ mui.plusReady(function(){ //复制链接到剪切板 var copy_content = ""; //判断是 ...