1123 Is It a Complete AVL Tree
四种旋转:
按照图的方向写:
Figure 12为rightrotation
和leftrotation
。
rlrotation
和lrrotation
,分别是先把根节点的右节点右旋再把根节点左旋转、先把根节点的左节点左旋再把根节点右旋转
层次遍历的同时判断是否为完整二叉树
当某一层没有左右子树(为NULL)时,激活标识after,然后如果后面还有继续下一层的push进队的动作,就说明不是完全二叉树了
int after=0,comp=1;
vector<int> layerorder(AVLTree *tree){
vector<int> res;
queue<AVLTree*> q;
q.push(tree);
while(!q.empty()){
AVLTree* now=q.front();
q.pop();
res.push_back(now->key);
if(now->left!=NULL){
q.push(now->left);
if(after) comp=0;
}else{
after=1;
}
if(now->right!=NULL){
q.push(now->right);
if(after) comp=0;
}else{
after=1;
}
}
return res;
}
AC代码:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node{
int v;
node *left,*right;
};
int getHeight(node *root){
if(root==NULL) return 0;
int l=getHeight(root->left);
int r=getHeight(root->right);
return max(l,r)+1;
}
node * r_rotation(node *root){
node *temp=root->left;
root->left=temp->right;
temp->right=root;
return temp;
}
node * l_rotation(node *root){
node *temp=root->right;
root->right=temp->left;
temp->left=root;
return temp;
}
node * lr_rotation(node *root){
root->left=l_rotation(root->left);
return r_rotation(root);
}
node * rl_rotation(node *root){
root->right=r_rotation(root->right);
return l_rotation(root);
}
void AVL_insert(node *&root,int temp){
if(root==NULL){
root=new node();
root->v=temp;
}else if(root->v>temp){
AVL_insert(root->left,temp);
int l=getHeight(root->left),r=getHeight(root->right);
if(l-r>=2){
if(root->left->v>temp)
root=r_rotation(root);
else
root=lr_rotation(root);
}
}else{
AVL_insert(root->right,temp);
int l=getHeight(root->left),r=getHeight(root->right);
if(r-l>=2){
if(root->right->v<temp)
root=l_rotation(root);
else
root=rl_rotation(root);
}
}
}
int main(){
// freopen("1.txt","r",stdin);
int n,temp;
scanf("%d",&n);
node *root=NULL;
for(int i=0;i<n;i++){
scanf("%d",&temp);
AVL_insert(root,temp);
}
vector<int> res;
queue<node *> q;
q.push(root);
bool after=0,flag=1;
while(!q.empty()){
node * f=q.front();
q.pop();
res.push_back(f->v);
if(f->left!=NULL){
q.push(f->left);
if(after) flag=false;
}else after=true;
if(f->right!=NULL){
q.push(f->right);
if(after) flag=false;
}else after=true;
}
for(int i=0;i<res.size();i++){
printf("%d%c",res[i],i==res.size()-1?'\n':' ');
}
printf("%s",flag?"YES":"NO");
}