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 subtrees of any node difer by at most one; if at any time they difer 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 N distinct 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
题目分析
已知平衡二叉树建树序列,求建树后的根节点
解题思路
1.建树(平衡二叉树insert节点)
2.打印根节点
易错点
左旋、右旋、插入节点方法,参数列表中要用指针引用node *&root,否则是值传递,方法中对root本身的修改不会在main函数中生效
Code
#include <iostream>
using namespace std;
struct node {
int data;
int heigh=0;
node * left=NULL;
node * right=NULL;
node() {}
node(int _data):data(_data) {
heigh=1;
}
};
int getHeigh(node * root) {
if(root==NULL)return 0;
return root->heigh;
}
void updateHeigh(node * root) {
root->heigh=max(getHeigh(root->left),getHeigh(root->right))+1;
}
void L(node * &root) {
//左旋
node * temp=root->right;
root->right=temp->left;
temp->left=root;
updateHeigh(root);
updateHeigh(temp);
root=temp;
}
void R(node * &root) {
//右旋
node * temp=root->left;
root->left=temp->right;
temp->right=root;
updateHeigh(root);
updateHeigh(temp);
root=temp;
}
int getBalanceFactor(node *root) {
return getHeigh(root->left)-getHeigh(root->right);
}
void insert(node * &root, int val) {
if(root==NULL) {
root=new node(val);
return;
}
if(val<root->data) {
insert(root->left,val);
updateHeigh(root);
if(getBalanceFactor(root)==2) {
if(getBalanceFactor(root->left)==1) {
//LL
R(root);
} else if(getBalanceFactor(root->left)==-1) {
//LR
L(root->left);
R(root);
}
}
} else {
insert(root->right,val);
updateHeigh(root);
if(getBalanceFactor(root)==-2) {
if(getBalanceFactor(root->right)==-1) {
//RR
L(root);
} else if(getBalanceFactor(root->right)==1) {
//RL
R(root->right);
L(root);
}
}
}
}
int main(int argc,char * argv[]) {
int n,m;
scanf("%d",&n);
node * root=NULL;
for(int i=0; i<n; i++) {
scanf("%d",&m);
insert(root,m);
}
printf("%d",root->data);
return 0;
}
PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]的更多相关文章
- 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)
题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...
- PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]
题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- 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 ...
- 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 ...
- 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 (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
随机推荐
- gitlab访问慢,出现502,特别卡,耗内存cpu解决办法
前言 浏览器访问gitlab的web页面,发现非常慢,并且很容易出现502问题.其中一个原因就是8080端口被tomcat占用,前面一篇已经更换了端口,但还是很慢.后来搜了下,原因是gitlab占用内 ...
- Spring AOP 管理事务
<aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* ...
- 刷题23. Merge k Sorted Lists
一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...
- 七十一、SAP中内表的修改,改一行数据,或一行的某个字段
一.SAP中内表的修改,只能通过工作区来修改,代码如下 二.效果如下
- vue学习(五)生命周期 的钩子函数
生命周期的钩子函数 主要有以下几种 beforeCreate created beforeMount mounted beforeUpdate updated activated deactivate ...
- tableau-参数
tableau参数可用在计算字段.筛选器和参考线中替换常量值得动态值. 三种方式:1.在计算字段中使用筛选器 案例动态替换计算字段中设定的目标值. 创建参数 以参数值创建计算字段 2.筛选器中使用参数 ...
- 八、CI框架之输出URI路径,相当于GET
一.controller中的代码如下: 二.我们打开一个路径,输出对应的路径URI的值 http://127.0.0.1/CodeIgniter-3.1.10/index.php/welcome/in ...
- 关于torch.norm函数的笔记
先看一下它的参数: norm(p='fro', dim=None, keepdim=False, dtype=None) p: the order of norm. 一般来说指定 $p = 1, 2$ ...
- Python win32api.keybd_event模拟键盘输入
win32api.keybd_event 该函数原型:keybd_event(bVk, bScan, dwFlags, dwExtraInfo) 第一个参数:虚拟键码(键盘键码对照表见附录): 第二个 ...
- Linux笔记01
linux目录结构 : linux只有一个目录. usr:等价于programfiles: etc:存放系统配置 root:管理员(超级用户)目录, home:存放其他用户的目录: lib:共享包: ...