就是AVL的模板题了 注意细节

#include<bits/stdc++.h>

using namespace std;
typedef struct node;
typedef node * tree;
struct node
{
int v;
int heigh;
tree L,R;
}; int getheigh(tree root)
{
if(root==NULL) return ;
return root->heigh;
} void updataheigh(tree root)
{
root->heigh=max(getheigh(root->L),getheigh(root->R))+;
} int getBalance(tree root)
{
return getheigh(root->L)-getheigh(root->R);
} void L(tree &root)
{
tree temp;
temp=root->R;
root->R=temp->L;
temp->L=root;
updataheigh(root);
updataheigh(temp);
root=temp;
} void R(tree &root)
{
tree temp;
temp=root->L;
root->L=temp->R;
temp->R=root;
updataheigh(root);
updataheigh(temp);
root=temp;
}
void insertt(tree &root,int v)
{
if(root==NULL){
root=new node;
root->v=v;
root->heigh=;
root->L=root->R=NULL;
return;
}
if(v<root->v){
insertt(root->L,v);
updataheigh(root);
if(getBalance(root)==){
if(getBalance(root->L)==){
R(root);
}
else if(getBalance(root->L)==-){
L(root->L);
R(root);
}
}
}
else{
insertt(root->R,v);
updataheigh(root);
if(getBalance(root)==-){
if(getBalance(root->R)==-){
L(root);
}
else if(getBalance(root->R)==){
R(root->R);
L(root);
}
}
} }
int main()
{
int n;
scanf("%d",&n);
int x;
tree root;
root=NULL;
for(int i=;i<n;i++){
scanf("%d",&x);
insertt(root,x);
}
printf("%d\n",root->v);
return ;
}

1066 Root of AVL Tree (25 分)(平衡二叉树)的更多相关文章

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

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

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

  4. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

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

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

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

  8. 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  9. PAT 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

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

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

随机推荐

  1. 【luogu P3275 [SCOI2011]糖果】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3275 把不等式 A > B 转化成 A - B >= 1或者 B - A <= -1再差分 ...

  2. 整理下react中常见的坑

    其实有些也不能算是坑,有些是react的规定,或者是react的模式和平常的js处理的方式不同罢了 1.setState()是异步的this.setState()会调用render方法,但并不会立即改 ...

  3. Block代替delegate,尽量使用block,对于有大量的delegate方法才考虑使用protocol实现.

    Block代替delegate,尽量使用block,对于有大量的delegate方法才考虑使用protocol实现. 1.Block语法总结及示例如下:         //1.普通代码块方式bloc ...

  4. Java的内存--内存溢出vs内存泄露(2)

    系统上线后,经常会出现内存不足等错误out of memory,很是头疼,决定要一探究竟 内存溢出 1. 定义及原因          内存溢出是指应用系统中存在无法回收的内存或使用的内存过多,最终使 ...

  5. ETO的公开赛T4《对抗水滴》 题解(BY 萌萌哒123456 )

    题意: 给你一个\(n*n\)的矩阵A,其中有\(T\)个元素不为零.定义矩阵内元素\((x,y)\)的能量值 \(E[x][y]=\sum_{i=1}^{x}\sum_{j=1}^{y}[A[i][ ...

  6. FBI树

    题目描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含"0&q ...

  7. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--G-旋转矩阵

    链接:https://www.nowcoder.com/acm/contest/90/G 来源:牛客网 1.题目描述 景驰公司自成立伊始,公司便将“推动智能交通的发展,让人类的出行更安全,更高效,更经 ...

  8. Flask中对MySQL的基本操作

    在Flask-SQLAlchemy中,插入.修改.删除操作,均由数据库会话管理. 会话用 db.session 表示.在准备把数据写入数据库前,要先将数据添加到会话中然后调用 commit() 方法提 ...

  9. 使用CSS3制作首页登录界面实例

    响应式设计 在这个页面中,使用下面3点来完成响应式设计 1.最大宽度 .设定了一个 max-width 的最大宽度,以便在大屏幕时兼容.: 2.margin : 30px auto; 使其保持时刻居中 ...

  10. 使用docker安装和运行常用的数据库和中间件

    mysql: docker pull mysql: docker run --name mysql -p : -v /usr/share/zoneinfo/Asia/Shanghai:/etc/loc ...