样例输入1:
5
88 70 61 96 120
样例输出1:
70
样例输入2:
7
88 70 61 96 120 90 65
样例输出2:
88
题意:将输入调整为平衡二叉树(AVL),输出根结点元素
解题思路:判断插入结点对现有结点的平衡因子的影响,进而进行LL,LR,RL,RR旋转
假设三个结点连接关系为A->B->C,C为新插入结点并使得A的平衡因子==2
若C在A的左孩子的左子树上,则对A与B进行LL旋转
若C在A的左孩子的右子树上,则对A,B,C进行LR旋转,可分解为首先对B与C进行RR旋转,再对A与C进行LL旋转
若C在A的右孩子的右子树上,则对A与B进行RR旋转
若C在A的右孩子的左子树上,则对A,B,C进行RL旋转,可分解为首先对B与C进行LL旋转,再对A与C进行RR旋转
用一个递推实现一下就可以了…
见代码:
#include <bits/stdc++.h>
typedef struct AVLTreeNode{
int Data;
AVLTreeNode *Left;
AVLTreeNode *Right;
int Height;
}nAVLTree , *pAVLTree;
pAVLTree AVLInsertion(int nodeValue , pAVLTree pAvl);
int GetALVHeight(pAVLTree);
pAVLTree SingleLeftRotation(pAVLTree);
pAVLTree DoubleLeftRotation(pAVLTree);
pAVLTree SingleRightRotation(pAVLTree);
pAVLTree DoubleRightRotation(pAVLTree);
int Max(int hight1 , int hight2);
using namespace std;
int main() {
int num;
int i;
int value;
pAVLTree pAvl;
pAvl = NULL;
cin >> num;
for (i