这道题是看了柳婼的解法才搞定的。开始想着把height和parent放到结构体中去,很繁琐最后还搞不定……
#include <cstdio>
#include <algorithm>
#include <vector>
struct node{
int key;
node* left = nullptr;
node* right = nullptr;
};
int N, t, pivot;
node* root = nullptr;
bool flag = true;
std::vector<node*> ans;
int getHeight(node* tree){
if(!tree){
return 0;
}
int l = getHeight(tree->left);
int r = getHeight(tree->right);
return std::max(l, r) + 1;
}
node* LL(node* tree){
node* tmp = tree->left;
tree->left = tmp->right;
tmp->right = tree;
return tmp;
}
node* RR(node* tree){
node* tmp = tree->right;
tree->right = tmp->left;
tmp->left = tree;
return tmp;
}
node* LR(node* tree){
tree->left = RR(tree->left);
return LL(tree);
}
node* RL(node* tree){
tree->right = LL(tree->right);
return RR(tree);
}
node* insertKey(node* tree, int k){
if(!tree){
node* tmp = new node;
tmp->key = k;
tree = tmp;
return tree;
}
int l, r;
if(k < tree->key){
tree->left = insertKey(tree->left, k);
l = getHeight(tree->left);
r = getHeight(tree->right);
if(l - r >= 2){
if(k < tree->left->key){
tree = LL(tree);
} else{
tree = LR(tree);
}
}
} else{
tree->right = insertKey(tree->right, k);
l = getHeight(tree->left);
r = getHeight(tree->right);
if(r - l >= 2){
if(k < tree->right->key){
tree = RL(tree);
} else{
tree = RR(tree);
}
}
}
return tree;
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%d", &t);
root = insertKey(root, t);
}
ans.push_back(root);
pivot = 0;
while(pivot < ans.size()){
if(ans[pivot]->left){
ans.push_back(ans[pivot]->left);
} else if(ans.size() != N){
flag = false;
}
if(ans[pivot]->right){
ans.push_back(ans[pivot]->right);
} else if(ans.size() != N){
flag = false;
}
++pivot;
}
for(int i = 0; i < ans.size(); ++i){
printf("%d", ans[i]->key);
printf("%s", i == ans.size() - 1 ? "\n" : " ");
}
printf("%s", flag ? "YES" : "NO");
return 0;
}
题目如下:
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 output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). 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, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YES
if the tree is complete, or NO
if not.
Sample Input 1:
5
88 70 61 63 65
Sample Output 1:
70 63 88 61 65
YES
Sample Input 2:
8
88 70 61 96 120 90 65 68
Sample Output 2:
88 65 96 61 70 90 120 68
NO