# 我真的不知道问题出在什么地方，这是关于AVL树的问题，称为“是否是完整的AVL树” [关闭]

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.

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

``````

``````#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

struct Node
{
Node* leftNode;
Node* rightNode;
int high;
int value;
}*root;

const int maxx = 21;
int n, m = 0, num, flag = 1, symbol = 0;

Node* newNode(int v) {
Node* node = new Node;
node->high = 1;
node->leftNode = node->rightNode = NULL;
node->value = v;
return node;
}

int getHeight(Node* root) {
if (root == NULL) return 0;
return root->high;
}

int getBalanceFactor(Node* root) {
return getHeight(root->leftNode) - getHeight(root->rightNode);
}

void updateHeight(Node* &root) {
root->high = max(getHeight(root->leftNode), getHeight(root->rightNode)) + 1;
}

void L(Node*& root) {
Node* tmp = root->rightNode;
root->rightNode = tmp->leftNode;
tmp->leftNode = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
}

void R(Node* root) {
Node* tmp = root->leftNode;
root->leftNode = tmp->rightNode;
tmp->rightNode = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
}

void insert(Node*& root, int v) {
if (root == NULL) {
root = newNode(v);
return;
}
if (v < root->value) {
insert(root->leftNode, v);
updateHeight(root);
if (getBalanceFactor(root) == 2) {
if (getBalanceFactor(root->leftNode) == 1) {
R(root);
}
else if (getBalanceFactor(root->leftNode) == -1) {
L(root->leftNode);
R(root);
}
}
}
else {
insert(root->rightNode, v);
updateHeight(root);
if (getBalanceFactor(root) == -2) {
if (getBalanceFactor(root->rightNode) == -1) {
L(root);
}
else if (getBalanceFactor(root->rightNode) == 1) {
R(root->rightNode);
L(root);
}
}
}
}

void LayerOrder(Node* root) {
queue<Node*> Q;
Q.push(root);
while (!Q.empty())
{
Node* top = Q.front();
Q.pop();
printf("%d", top->value);
m++;
if (m < n) printf(" ");
if (symbol && top->leftNode != NULL) flag = 0;
if (top->leftNode != NULL && top->rightNode == NULL) symbol = 1;
if (top->leftNode == NULL && top->rightNode != NULL) flag = 0;
if (top->leftNode != NULL) Q.push(top->leftNode);
if (top->rightNode != NULL) Q.push(top->rightNode);
}
}

void Delete(Node*& root) {
if (root != NULL) {
Delete(root->leftNode);
Delete(root->rightNode);
delete root;
}
}

int main() {

scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &num);
insert(root, num);
}
LayerOrder(root);
if (flag) printf("\nYES");
else printf("\nNO");
Delete(root);

return 0;

}
``````

You can also see my code in Coliru