# 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 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树首先是一棵搜索树，所以根的左边都会比根小，根的右边都会比根大，尝试了下投机取巧排序输出中位数，这题能拿17分（逃

#include <algorithm>
using namespace std;
const int maxn=20;
int input[maxn];
int main()
{
int N;
cin>>N;
for(int i=0;i<N;i++)
{
cin>>input[i];
}
sort(input,input+N);
if(N%2!=0)
cout<<input[(N-1)/2];
else
cout<<input[N/2];
return 0;
}


A发现不平衡的情况有四种：

• B插入到A的左子树的左子树
• B插入到A的左子树的右子树
• B插入到A的右子树的左子树
• B插入到A的右子树的右子树

head->height=maxHeight(getHeight(head->rightChild),getHeight(head->leftChild))+1;

#include <iostream>

using namespace std;

struct node
{
int value;
node* rightChild;
node* leftChild;
int height;
};
int maxHeight(int temp1,int temp2)
{
return temp1>temp2?temp1:temp2;
}
int getHeight(node* root)
{
if(root==NULL)
return 0;
else
return root->height;
}
node* RR(node* root)
{
node* temp=root;
root=root->rightChild;
temp->rightChild=root->leftChild;
root->leftChild=temp;
temp->height=maxHeight(getHeight(temp->rightChild),getHeight(temp->leftChild))+1;
root->height=maxHeight(getHeight(root->rightChild),getHeight(root->leftChild))+1;
return root;
}
node* LL(node* root)
{
node* temp=root;
root=root->leftChild;
temp->leftChild=root->rightChild;
root->rightChild=temp;
temp->height=maxHeight(getHeight(temp->rightChild),getHeight(temp->leftChild))+1;
root->height=maxHeight(getHeight(root->rightChild),getHeight(root->leftChild))+1;
return root;
}
node* RL(node* root)
{
root->rightChild=LL(root->rightChild);
return RR(root);
}
node* LR(node* root)
{
root->leftChild=RR(root->leftChild);
return LL(root);
}
{
{
node* temp=new node();
temp->value=value;
temp->height=1;
return temp;
}
{
{
{
}
else
{
}
}
}
else
{
{
{
}
else
{
}
}
}
}
int main()
{
int N;
cin>>N;
node* tree=NULL;
for(int i=1;i<=N;i++)
{
int value;
cin>>value;
tree=buildTree(value,tree);
}
cout<<tree->value;
return 0;
}