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. weblogic 配置了ssl

    jingyan.baidu.com/article/72ee561abfe531e16138dfb5.html http://blog.sina.com.cn/s/blog_7ffec3e201019 ...

  2. webgote的例子(3)Sql注入(SearchPOST)

    Sql注入(Search/POST) (本章内容):post的方式进行注入 今天来讲一下sql注入的另一个例子(post) 上一个用的是get请求的方法将我们的参数传到服务器进行执行 上图中的标红是需 ...

  3. js日期工具

    /** * 日期工具类 */ define(function(require, exports, module) { var constants = require("constants&q ...

  4. ifconfig与内核通信 ifreq 结构体分析和使用

    结构原型: /* * Interface request structure used for socket * ioctl's.  All interface ioctl's must have p ...

  5. 转载:Logistic回归原理及公式推导

    转载自:AriesSurfer 原文见 http://blog.csdn.NET/acdreamers/article/details/27365941 Logistic回归为概率型非线性回归模型,是 ...

  6. 三十分钟理解博弈论“纳什均衡” -- Nash Equilibrium

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 纳什均衡(或者纳什平衡),Nash ...

  7. Linux 用户篇——用户管理命令之id、whoami、su、chage

    一.浅谈id.whoami.su.chage 本篇是续写上一篇<Linux 用户篇——用户管理命令之useradd.passwd.userdel.usermod>. (1)id命令 命令格 ...

  8. CSU 1412 Line and Circles

    原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1412 题目要求判断是否有一条直线可以穿过所有的圆. 做法:把所有圆心做一次凸包,然后判断 ...

  9. 深入理解 WordPress 数据库中的用户数据 wp_user

    WordPress 使用 wp_users 数据表存储用户的主要数据,该数据表结构类似于wp_posts 和 wp_comments 数据表,存储的是需要经常访问的用户数据,该数据表的结构以及该数据表 ...

  10. Loadrunner脚本开发规范

    Loadrunner脚本开发规范 目录 1.一般约定... 3 2.代码注释约定... 4 3.格式化代码... 5 1.一般约定 1.1具体脚本规则,必须在具体代码中加注释,以便脚本开发人员阅读和理 ...