1123 Is It a Complete AVL Tree (30分)
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
题目描述:完全平衡二叉树。具体参考1066的题解
该题需要建树,判断是否能形成完全平衡二叉树,是输出YES,否输出NO。还要输出树的层序遍历。
解题思路:建树,完成左转、右转、左右转、右左转的操作、求结点的高度等,最后利用BFS得到层序遍历。
#include<bits/stdc++.h>
using namespace std;
int n, k;
vector<int> level;
struct node{
int num;
node *l, *r;
node(){
l = r = NULL;
}
};
node *LeftRoute(node *root){
node *t;
t = root->r;
root->r = t->l;
t->l = root;
return t;
}
node *RightRoute(node *root){
node *t;
t = root->l;
root->l = t->r;
t->r = root;
return t;
}
node *LeftRightRoute(node *root){
root->l = LeftRoute(root->l);
root = RightRoute(root);
return root;
}
node *RightLeftRoute(node *root){
root->r = RightRoute(root->r);
root = LeftRoute(root);
return root;
}
int height(node *root){
if(root==NULL) return 1;
return max(height(root->l),height(root->r))+1;
}
node *Insert(node *root, int k){
if(root==NULL){
root = new node();
root->num = k;
root->l = root->r = NULL;
}
else if(k>=root->num){
root->r = Insert(root->r,k);
if(abs(height(root->r)-height(root->l))>=2){
root = k>=root->r->num ? LeftRoute(root) : RightLeftRoute(root);
}
}
else{
root->l = Insert(root->l,k);
if(abs(height(root->l)-height(root->r))>=2){
root = k<=root->l->num ? RightRoute(root) : LeftRightRoute(root);
}
}
return root;
}
void bfs(node *root){
pair<node *, int> t, tt;
queue<pair<node *, int> > q;
t.first = root; t.second = 1;
q.push(t);
bool flag = true;
int cnt = 1;
while(!q.empty()){
t = q.front(); q.pop();
if(cnt!=t.second) flag = false;
cnt++;
level.push_back(t.first->num);
if(t.first->l!=NULL){
tt.first = t.first->l;
tt.second = t.second*2;
q.push(tt);
}
if(t.first->r!=NULL){
tt.first = t.first->r;
tt.second = t.second*2+1;
q.push(tt);
}
}
for(int i=0; i<level.size(); i++){
if(i) printf(" ");
printf("%d", level[i]);
}
printf("\n");
if(flag) printf("YES\n");
else printf("NO\n");
}
int main()
{
node *tree = NULL;
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%d", &k);
tree = Insert(tree,k);
}
bfs(tree);
return 0;
}