下次有时间了再去找里面的bug吧
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
struct node
{
int data;
node *left,*right;
int height;
};
node* newNode(int x)
{
node *root =new node;
root->data=x;
root->height=1;
root->left=root->right=NULL;
return root;
}
int getHeight(node *root)
{
if(root==NULL)
{
return 0;
}
return root->height;
}
int getBalancefactor(node* root)
{
return getHeight(root->left)-getHeight(root->right);
}
void updateHeight(node* root)
{
root->height=max(getHeight(root->left),getHeight(root->right))+1;
}
void L(node* &root)
{
node* temp=root->right;
root->right=temp->left;
temp->left=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
void R(node* &root)
{
node* temp=root->left;
root->left=temp->right;
temp->right=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
void insert(node* &root,int x)
{
if(root==NULL)
{
root=newNode(x);
return;
}
else
{
if(x<root->data)
{
insert(root->left,x);
updateHeight(root);
if(getBalancefactor(root)==2)
{
if(getBalancefactor(root->left)==1)
{
R(root);
}
else if(getBalancefactor(root->right)==-1)
{
L(root->right);
R(root);
}
}
}
else
{
insert(root->right,x);
updateHeight(root);
if(getBalancefactor(root)==-2)
{
if(getBalancefactor(root->left)==-1)
{
L(root);
}
else if(getBalancefactor(root->right)==1)
{
R(root->right);
L(root);
}
}
}
}
}
int main()
{
int num;
scanf("%d",&num);
node* root=NULL;
for(int i=0; i<num; i++)
{
int x;
scanf("%d",&x);
insert(root,x);
}
printf("%d\n",root->data);
}