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. SVM问题再理解与分析——我的角度

    SVM问题再理解与分析--我的角度 欢迎关注我的博客:http://www.cnblogs.com/xujianqing/ 支持向量机问题 问题先按照几何间隔最大化的原则引出他的问题为 上面的约束条件 ...

  2. for 、forEach 、 forof、 forin遍历对比

    一.遍历内容的异同 1.for 和 for...in 是针对数组下标的遍历 2.forEach 及 for...of 遍历的是数组中的元素 二.对非数字下标的处理 由于array在js中也是对象中的一 ...

  3. Machine Learning系列--深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件

    在求取有约束条件的优化问题时,拉格朗日乘子法(Lagrange Multiplier) 和KKT条件是非常重要的两个求取方法,对于等式约束的优化问题,可以应用拉格朗日乘子法去求取最优值:如果含有不等式 ...

  4. C#调用mciSendString播放音频文件

    mciSendString函数是一个WinAPI,主要用来向MCI(Media Control Interface)设备发送字符串命令. 一.函数的声明如下: private static exter ...

  5. 大型网站的 HTTPS 实践(一)—— HTTPS 协议和原理(转)

    原文链接:http://op.baidu.com/2015/04/https-s01a01/ 1 前言 百度已经于近日上线了全站 HTTPS 的安全搜索,默认会将 HTTP 请求跳转成 HTTPS.本 ...

  6. 详述Linux配置静态IP、设置DNS和主机名(一)

    Linux配置静态IP.设置DNS和主机名首先要找到配置文件,这是在Linux系统下进行工作的必须知道工作方式.后面一步步的跟着这个范例来进行配置相信你最终也会完成Linux配置静态IP.设置DNS和 ...

  7. LightOJ 1414 February 29(闰年统计+容斥原理)

    题目链接:https://vjudge.net/contest/28079#problem/R 题目大意:给你分别给你两个日期(第二个日期大于等于第一个日期),闰年的2月29日称为闰日,让你求两个日期 ...

  8. appium---【Mac】Appium-Doctor提示WARN:“ opencv4nodejs cannot be found”解决方案

    报错提示: AppiumDoctor  ✖. opencv4nodejs cannot be found 如下截图: 解决方案: 第一步:安装cmake并配置环境变量 1.打开terminal执行:c ...

  9. Centos bash: make: command not found

    一般出现这个bash: make: command not found提示,是因为安装系统的时候使用的是最小化mini安装,系统没有安装make.vim等常用命令,直接yum安装下即可: yum -y ...

  10. Mybatis处理列名—字段名映射— 驼峰式命名映射

    规范命名,数据库字段名使用 : 下划线命名(user_id) 类属性使用 : 驼峰命名(userId) 配置mybatis 时,全局设置: <settings> <!-- 开启驼峰, ...