PAT甲级1066 Root of AVL Tree (25 分)

PAT甲级真题 1066 Root of AVL Tree (25分) C++实现(建立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 differ by at most one; if at any time they differ by more than one, rebalancing... 阅读详情

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

 AVL树对我来说一直整不明白,太难了。

输出中位数也可以得到大部分结果,舒服了

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 30;
int n;
int num[maxn];

int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &num[i]);
    }
    sort(num + 1, num + n + 1);
    if(n % 2 == 0)
        printf("%d", num[n / 2] );
    else
        printf("%d", num[ (n + 1) / 2] );
}

多重括号,容易发生错误 

#include<cstdio>
#include<algorithm>
using namespace std;
struct node {
	int v, height;
	node* lchild, * rchild;
}*root;

node* newNode(int v) {
	node* Node = new node;
	Node->v = v;
	Node->height = 1;
	Node->lchild = Node->rchild = NULL;
	return Node;
}

int getHeight(node* root) {
	if (root == NULL) return 0;
	return root->height;
}
void updateHeight(node* root) {
	root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
}
int getBalanceFactor(node* root) {
	return getHeight(root->lchild) - getHeight(root->rchild);
}

void R(node*& root) {
	node* temp = root->lchild;
	root->lchild = temp->rchild;
	temp->rchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void L(node*& root) {
	node* temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void insert(node*& root, int v) {
	if (root == NULL) {
		root = newNode(v);
		return;
	}
	if (v < root->v) {
		insert(root->lchild, v);
		updateHeight(root);
		if (getBalanceFactor(root) == 2) {
			if (getBalanceFactor(root->lchild) == 1) { // LL型
				R(root);
			}
			else if (getBalanceFactor(root->lchild) == -1) {// LR
				L(root->lchild);
				R(root);
			}
		}
	}
		else {
			insert(root->rchild, v);
			updateHeight(root);
			if (getBalanceFactor(root) == -2) {
				if (getBalanceFactor(root->rchild) == -1) {
					L(root);
				}
				else if (getBalanceFactor(root->rchild) == 1) {
					R(root->rchild);
					L(root);
				}
			}
		}
	
}


int main() {
	int n, v;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &v);
		insert(root, v);
	}
	printf("%d\n", root->v);
	return 0;
}

 

告别版本过低提示!微信3.9登录修复与聊天记录无缝迁移全攻略 本文详细解析了微信3.9版本过低提示的根源,并提供了完整的修复方案和聊天记录迁移指南。通过专用修复工具绕过版本检测,以及新旧版本聊天记录目录对比,帮助用户实现无缝迁移。特别提醒备份重要数据,确保操作安全。 阅读详情

相关推荐

最新版电子发票样式html+css--普通发票+增值发票

【代码】最新版电子发票样式html+css--普通发票+增值发票。

weixin_44110923的博客 3691

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 tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by mo...

柳婼 の blog 4310

MATLAB 设置legend加标题

x1 = linspace(0,5); y1 = sin(x1/2); plot(x1,y1) hold on x2 = [0 1 2 3 4 5]; y2 = [0.2 0.3 0.6 1 0.7 0.6]; scatter(x2,y2,'filled') hold off legend({'sin(x/2)','2016'},'Location','northwest','Orientation','horizontal')%水平 legend添加标题 lgd = legend; lgd.Fon

早起CaiCai的学习空间 4714

PATA 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, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by ...

热心市民小黎的博客 662

【PLL】分频器设计权衡:从CMOS到CML的电路实现与优化

本文深入探讨了PLL中分频器的设计权衡,比较了CMOS和CML两种电路实现的优缺点。通过实际案例分析,展示了如何在速度、功耗和噪声之间取得平衡,特别是在5G射频芯片和Wi-Fi 6E等高频应用中的优化实践。文章还提供了混合架构设计和电源噪声协同优化的实用技巧,适合电路设计工程师参考。

weixin_42521843的博客 444

SpringBoot一次请求中缓存数据,请求结束就清除的4种方法,你会几种?

通过以上四种方法,你可以在 SpringBoot 的一次请求中缓存数据,并在请求结束后自动清除这些缓存数据。使用 ThreadLocal:适用于需要跨多个方法调用的缓存数据,灵活性高,但需要注意内存泄漏问题。使用 AOP:适用于全局性的缓存管理,代码更加优雅,但需要对 AOP 有一定的了解。使用 ServletRequestListener:适用于简单的请求生命周期管理,配置简单,但功能相对有限。使用 HttpServletRequest 的 setAttribute 和 getAttribute。

java专栏 1295

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 subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...

Young's Blog 245

Colmap论文——《Structure-from-Motion Revisited》论文阅读笔记

  最近又将Colmap论文翻出来仔细阅读总结了一下,于是顺便写个博客记录一下。Structure-from-Motion Revisited是当前SOTA的增量式SfM算法Colmap的论文,发表于2016年计算机视觉顶会CVPR。它是增量式SfM里程碑式的作品。 论文地址:https://openaccess.thecvf.com/content_cvpr_2016/papers/Schonberger_Structure-From-Motion_Revisited_CVPR_2016_paper.p

zeeq的博客 1万+

**04-树5 Root of AVL Tree (25 分)** 初识二叉平衡树

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 subtrees of any node differ by at most one; if at any time they differ by...

weixin_38732825的博客 416

12、有杆泵抽油系统:原理、性能与选择

本文全面介绍了有杆泵抽油系统的原理、性能分析和设备选择方法。有杆泵抽油系统广泛应用于石油开采领域,通过地面抽油装置与井下柱塞泵的配合,实现高效、稳定的原油提升。文章详细解析了系统的工作原理、组成结构、光杆运动特性、载荷计算、泵输送能力及功率需求,并提供了抽油装置的选择流程和实际案例分析。此外,还探讨了泵的容积效率影响因素、示功图的作用及系统优化运行的建议,为石油开采工程提供了理论支持和实践指导。

gan8painter的博客 435

图像处理系列——直方图之直方图规定化(Histogram Specification)

直方图规定化也叫直方图匹配(Histogram Matching),让原图像的像素分布与规定图像的像素分布保持一致。直方图规定化比直方图均衡化更加灵活,算是直方图均衡的变种。常用于图片风格一致性的自动处理,能有效的改善图像的整体或局部的特征,广泛运用在图像增强处理中。

nanke_yh的博客 4595

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 subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...

DayDream_x的博客 250

YOLO26涨点改进 | 全网独家首发 主干改进篇 | TGRS 2025 | RepViT-TD,告别庞大骨干!YOLO26 + RepViT-TD轻量又强大,实现更快更准的小目标检测!

本文提出将RepViT-TD轻量级主干网络应用于YOLO26目标检测框架,以提升小目标检测性能。RepViT-TD通过优化计算资源分配,强化前两层高分辨率特征提取能力,在保持轻量化的同时(仅4.85M参数)显著提升微小目标的检测精度(AI-TODv2数据集AP50达54.6%)。文章详细介绍了三种主干改进方案(RepVit_ldy1/2/3)的代码实现,并提供了完整的YAML配置文件修改指南,使模型能高效部署于边缘设备。实验表明,该方法在计算复杂度(38.19G FLOPs)和检测精度间取得了良好平衡。

Ai缝合怪、助力小伙伴高效发论文 329

【PAT甲级】1066 Root of AVL Tree (25 分)

PAT甲级1066题解 第5个测试点未通过

小染的学习笔记 361

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

PAT甲级1066 Root of AVL Tree (25 分)

1066Root of AVL Tree(25分) 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...

Dexter 198

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

PAT甲级-1066 Root of AVL Tree (25分)

点击链接PAT甲级-AC全解汇总 题目: 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 tha...

卷子的博客 288

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

甲级PAT 1066 Root of AVL Tree (25 分)(AVL)

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 subtrees of any node differ by at most one; if at any time they differ by...

Joyceyang_999的博客 294

【PAT甲级A1066】Root of AVL Tree (25分)(c++)

1066 Root of AVL Tree (25分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB 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

小庄同学的博客 205

【PAT甲级】1066 Root of AVL Tree (25分)

解题过程的小记录,如有错误欢迎指出。 难度:五星(此题涵盖了几乎所有算法笔记上关于AVL树的函数,值得一背) 小导航~题目分析注意点我的解题过程思路bug代码dalao的代码借鉴点 题目分析 给出数列的插入顺序,要求构造AVL树,构成完成后输出此树的根结点的数据域 注意点 因为AVL树是基于BST基础的,题目要求输出根结点数据域,直接输出数列的中位数也可以拿到一定的分数 我的解题过程 思路 把插入数列保存在vector数组里 一系列AVL相关操作,具体看代码 bug 在右旋操作函数中,更新高度的顺序搞反

SpicyChicken的自我修养 405

PAT甲级A1066 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 subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...

vir_lee的博客 223

PAT-甲级-1066 Root of AVL Tree (25 分)

1066Root of AVL Tree(25分) 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...

valcanoshan的博客 234

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 subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...

smilng的博客 185

PAT 甲级 1066 Root of AVL Tree (25 分) AVL入门讲解

题意: AVL建树,输出根节点 思路: AVL建树过程中四种平衡方式是难点,但是理解之后就可以比较容易的写出来了 node结构体表示树的结点,存储:值,左右子树高度;左孩子,右孩子; 其中LL型跟RR型相似的写法;LR型RL型写法比较相似; 下面讲解一下LL型和LR型: LL型:设当前不平衡结点为根节点,(左孩子的左子树更大所谓LL型),这时候根据BST性质,需要调整根节点以及根节点...

床前明月光 229

PAT甲级1066 Root of AVL Tree (25分)|C++实现

一、题目描述 原题链接 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 n

weixin_42393947的博客 157

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 subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...

猪の成长日记 204

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 subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...

一只奋斗中的本科生 227
上一篇: PAT甲级 - 1099 Build A Binary Search Tree (30 分) 重建二叉搜索树
下一篇: PAT甲级 - 1107 Social Clusters (30 分) 并查集
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值