平衡二叉树问题,本题考察的知识点有,平衡二叉树的生成,左旋,右旋,如何判断一个树是否是完全二叉树,二叉树的层序遍历。
坑点如下
1,将左旋函数和右旋函数写反了导致段错误。
整体代码如下
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct Node{
int data,height;
Node* lchild;
Node* rchild;
};
Node* newnode(int v){
Node* root=new Node;
root->height=1;
root->data=v;
root->lchild=root->rchild=NULL;
return root;
}
int getheight(Node* root){
if(root==NULL)return 0;
return root->height;
}
int getbalancef(Node* root){
return getheight(root->lchild)-getheight(root->rchild);
}
void r(Node* &root){
Node* temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
root->height= max(getheight(root->lchild),getheight(root->rchild))+1;
temp->height= max(getheight(temp->lchild),getheight(temp->rchild))+1;
root=temp;
}
void l(Node* &root){
Node* temp=root->rchild;
root->rchild=temp->lchild;
temp->lchild=root;
root->height= max(getheight(root->lchild),getheight(root->rchild))+1;
temp->height= max(getheight(temp->lchild),getheight(temp->rchild))+1;
root=temp;
}
void insert(Node* &root,int data){
if(root==NULL){
root=newnode(data);
return;
}
if(data<root->data){
insert(root->lchild,data);
root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
if(getbalancef(root)==2){
if(getbalancef(root->lchild)==1)
{
r(root);
}
else if(getbalancef(root->lchild)==-1)
{ l(root->lchild);
r(root);
}
}
}
else
{ insert(root->rchild,data);
root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
if(getbalancef(root)==-2){
if(getbalancef(root->rchild)==-1)
{ l(root);
}
else if(getbalancef(root->rchild)==1)
{ r(root->rchild);
l(root);
}
}
}
}
int maxnow=-1;
void DFS(Node* &root,int index){
if(index>maxnow)maxnow=index;
if(root->lchild!=NULL)DFS(root->lchild,index*2);
if(root->rchild!=NULL) DFS(root->rchild,index*2+1);
}
int main(){
int n,data;
scanf("%d",&n);
Node* root=NULL;
for(int i=0;i<n;i++){
scanf("%d",&data);
insert(root,data);
}
DFS(root,1);
int cnt=0;
queue<Node*>Q;
Q.push(root);
while(!Q.empty()){
Node* node=Q.front();
Q.pop();
printf("%d",node->data);
cnt++;
if(cnt<n)printf(" ");
if(node->lchild!=NULL)Q.push(node->lchild);
if(node->rchild!=NULL)Q.push(node->rchild);
}
printf("\n");
printf("%s\n",maxnow==n?"YES":"NO");
return 0;
}
二刷代码如下
坑点:注意左旋右旋中更新高度的顺序,先更新底下的高度,后更新上方高度,对应测试点三
#include<bits/stdc++.h>
using namespace std;
int n,v,mindex=-1,cnt=0;
struct Node{
Node* l;
Node* r;
int data,h;
};
int geth(Node* root){
if(root==NULL)return 0;
return root->h;
}
int getbalancef(Node* root){
return geth(root->l)-geth(root->r);
}
void r(Node* &root){
Node* temp=root->l;
root->l=temp->r;
temp->r=root;
root->h=max(geth(root->l),geth(root->r))+1;//此处高度更新顺序不能变动
temp->h=max(geth(temp->l),geth(temp->r))+1;
root=temp;
}
void l(Node* &root){
Node* temp=root->r;
root->r=temp->l;
temp->l=root;
root->h=max(geth(root->l),geth(root->r))+1;//此处高度更新顺序不能变动
temp->h=max(geth(temp->l),geth(temp->r))+1;
root=temp;
}
void insert(Node* &root,int data){
if(root==NULL){
root=new Node;
root->data=data;
root->h=1;
root->l=root->r=NULL;
return;
}
if(data<root->data){
insert(root->l,data);
root->h=max(geth(root->l),geth(root->r))+1;
if(getbalancef(root)==2){
if(getbalancef(root->l)==1)
r(root);
else if(getbalancef(root->l)==-1){
l(root->l);
r(root);
}
}
}
else {
insert(root->r,data);
root->h=max(geth(root->l),geth(root->r))+1;
if(getbalancef(root)==-2){
if(getbalancef(root->r)==-1)
l(root);
else if(getbalancef(root->r)==1){
r(root->r);
l(root);
}
}
}
}
void dfs(Node* root,int index){
if(root==NULL)return;
if(index>mindex)mindex=index;
dfs(root->l,index*2);
dfs(root->r,index*2+1);
}
void level(Node* &root){
queue<Node*>Q;
Q.push(root);
while(!Q.empty()){
Node* top=Q.front();
Q.pop();
printf("%d",top->data);
cnt++;
printf("%s",cnt==n?"\n":" ");
if(top->l!=NULL)Q.push(top->l);
if(top->r!=NULL)Q.push(top->r);
}
}
int main(){
scanf("%d",&n);
Node* root=NULL;
for(int i=0;i<n;i++)
{ scanf("%d",&v);
insert(root,v);}
dfs(root,1);
level(root);
printf("%s\n",mindex==n?"YES":"NO");
return 0;
}