PAT甲级1066. Root of AVL Tree

题意:

构造AVL树,返回root点val。

思路:

了解AVL树的基本性质。

AVL树

ac代码:

C++

// pat1066.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map> using namespace std; struct AvlNode
{
int data;
AvlNode* left;
AvlNode* right;
int height;
AvlNode(int x) : data(x), left(NULL), right(NULL), height(0) {}
}; int height(AvlNode* root)
{
return root == NULL ? -1 : root->height;
} void LLRotate(AvlNode*& root) //右单
{
AvlNode* temp = root->left;
root->left = temp->right;
temp->right = root; temp->height = max(height(temp->left), height(temp->right)) + 1;
root->height = max(height(root->left), height(root->right)) + 1; root = temp;
} void RRRotate(AvlNode*& root) //左单
{
AvlNode* temp = root->right;
root->right = temp->left;
temp->left = root; temp->height = max(height(temp->left), height(temp->right)) + 1;
root->height = max(height(root->left), height(root->right)) + 1; root = temp;
} void RLRotate(AvlNode*& root)
{
LLRotate(root->right); RRRotate(root);
} void LRRotate(AvlNode*& root)
{
RRRotate(root->left); LLRotate(root);
} void insert(const int& x, AvlNode*& root)
{
if (!root)
{
root = new AvlNode(x);
}
else if (x < root->data)
{
insert(x, root->left);
if (height(root->left) - height(root->right) == 2)
{
if (x < root->left->data) LLRotate(root);
else LRRotate(root);
}
}
else if (x > root->data)
{
insert(x, root->right);
if (height(root->right) - height(root->left) == 2)
{
if (x > root->right->data) RRRotate(root);
else RLRotate(root);
}
} root->height = max(height(root->left), height(root->right)) + 1;
} int main()
{
AvlNode* root = NULL;
int N;
int i;
int num[22]; scanf("%d", &N); for (int i = 0; i < N; i++)
{
scanf("%d", &(num[i]));
} for (int i = 0; i < N; i++)
{
insert(num[i], root);
} printf("%d", root->data);
return 0;
}

PAT甲级1066. Root of AVL Tree的更多相关文章

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

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

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

  3. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

  4. PAT甲级——A1066 Root of AVL Tree

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

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

  6. pat(A) 1066. Root of AVL Tree

    代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> # ...

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

  8. PAT 1066 Root of AVL Tree[AVL树][难]

    1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...

  9. PTA (Advanced Level) 1066 Root of AVL Tree

    Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...

随机推荐

  1. python基础===猴子补丁

    >>> class test: def A(self, x, y): return x+y >>> t = test() >>> t.A(10,2 ...

  2. B2旅游签证记

    先去https://ceac.state.gov/ceac/,选择DS-160表格,在线申请登记个人信息 ,选择大事馆“CHINA BEIJING”和验证码,点 Start an Applicatio ...

  3. WEB-INF下资源访问问题

    原文链接:http://blog.csdn.net/u010921701/article/details/67637527 问题描述:建了一个Web工程,将js.css等放到了WEB-INF文件下,发 ...

  4. java基础6 面向对象的详解

    本文知识点(目录): 1.1.万物皆对象    1.2.面向对象的概述    1.3.面向对象(java语言)与面向过程(C语言)对比    1.4.面向过程    1.5.对象    1.6.面向对 ...

  5. Spring mvc知识点总结——面试篇

    一.MVC思想MVC(Model-View-Controller)三元组的概念:1.Model(模型):数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或JavaBean组件(包含数 ...

  6. Web前端开发最佳实践(1):前端开发概述

    引言 我从07年开始进入博客园,从最开始阅读别人的文章到自己开始尝试表达一些自己对技术的看法.可以说,博客园是我参与技术讨论的一个主要的平台.在这其间,随着接触技术的广度和深度的增加,也写了一些得到了 ...

  7. java 获取路径的各种方法

    (1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...

  8. 第六章:加载或保存JSON数据

    加载或保存JSON数据 Knockout可以实现很复杂的客户端交互,但是几乎所有的web应用程序都要和服务器端交换数据(至少为了本地存储需要序列化数据),交换数据最方便的就是使用JSON格式 – 大多 ...

  9. 将 sublime test3 添加到鼠标右键的方法

    将 sublime test3 添加到鼠标右键的方法 将 sublime_addright.inf 文件放到 sublime text 安装目录进行安装即可. 文件名:sublime_addright ...

  10. thinkphp5.0命名空间

    命名空间 ThinkPHP采用命名空间方式定义和自动加载类库文件,有效的解决了多模块和Composer类库之间的命名空间冲突问题,并且实现了更加高效的类库自动加载机制. 如果不清楚命名空间的基本概念, ...