PAT a1123

目的:构建AVL树,然后判断是否是完全二叉树

输入:

一串节点序列

输出:

层序序列

以及判断是否是满二叉树 

#include<stdio.h>
#include<algorithm>
#include<queue>

using namespace std;

struct node{
    int val,height;
    node* left,*right;
};

node* newnode(int v)
{
    node* now = new node;
    now->height = 1;
    now->val = v;
    now->left = NULL;
    now->right= NULL;

    return now;
}
int getheight(node* root)
{
    if(root==NULL) return 0;
    return root->height;
}
int getbalancefactor(node* root)
{
    //获得左右子树的高度差
    return getheight(root->left)-getheight(root->right);
}
void updateheight(node* root)
{
    root->height = max(getheight(root->left),getheight(root->right))+1;//要是和getheight互换,那么每次查询高度差都要计算,可能会超时。更新计算
}
void L(node* &root)
{
    node* temp = root->right;
    root->right = temp->left;
    temp->left = root;
    updateheight(root);
    updateheight(temp);
    root = temp;
}
void R(node* &root)
{
    node* temp = root->left;
    root->left = temp->right;
    temp->right= root;
    updateheight(root);
    updateheight(temp);
    root = temp;
}
void insert(node* &root,int x)
{
    if(root==NULL)
    {
        root = newnode(x);
        return;
    }

    if(x < root->val)
    {
        insert(root->left,x);
        updateheight(root);
        if(getbalancefactor(root)==2)
        {
            if(getbalancefactor(root->left)==1)
            {
                R(root);
            }else
            {
                L(root->left);
                R(root);
            }
        }
    }else{
        insert(root->right,x);
        updateheight(root);
        if(getbalancefactor(root)==-2)
        {
            if(getbalancefactor(root->right)==-1)
            {
                L(root);
            }else
            {
                R(root->right);
                L(root);
            }
        }
    }
}
bool BFS(node* root)
{
    bool flag = true;
    int tag = 0;
    queue<node*> q;
    q.push(root);
    while(!q.empty())
    {
        node* top = q.front();
        printf("%d",top->val);
        q.pop();
        if(top->left!=NULL)
        {
            q.push(top->left);
            if(tag==1)
            {
                flag = false;
            }
        }else
        {
            if(tag==0)
            {
                tag=1;
            }
        }
        if(top->right!=NULL)
        {
            q.push(top->right);
            if(tag==1)
            {
                flag = false;
            }
        }else
        {
            if(tag==0)
            {
                tag=1;
            }
        }
        if(!q.empty())
        {
            printf(" ");
        }else
        {
            printf("\n");
        }
    }
    return flag;
}
int main()
{
    int N;
    scanf("%d",&N);

    node* root = NULL;

    for(int i=0;i<N;i++)
    {
        int x;
        scanf("%d",&x);
        insert(root,x);
    }
    if(BFS(root))
    {
        printf("YES\n");
    }else
    {
        printf("NO\n");
    }
    return 0;
}

反思:忘记了AVL怎么建树了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值