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怎么建树了。

### 关于PAT乙级1123题的C++实现 目前未提供具体关于PAT乙级1123题的相关引用内容。然而,基于常见的PAT乙级题目结构以及类似的解题思路[^1],可以推测该类问题通常涉及输入处理、数据存储与逻辑运算。 #### 假设场景:微博转发抽奖 如果假设PAT乙级1123题类似于微博转发抽奖问题(如引用[1]所示),则其核心在于记录参与者的ID并按照特定规则筛选获奖者。以下是可能的解决方案: ```cpp #include <iostream> #include <map> using namespace std; int main() { int m, n, s; cin >> m >> n >> s; // 输入总人数、间隔步数和起始位置 string id; map<string, int> participants; // 记录参与者及其状态 bool hasWinner = false; for (int i = 1; i <= m; ++i) { cin >> id; if (participants[id] == 1) { // 如果重复,则跳过当前轮次 s += 1; } if (i >= s && participants[id] == 0) { // 判断是否到达指定位置且未曾中奖 participants[id] = 1; cout << id << endl; hasWinner = true; s += n; // 更新下一次抽奖的位置 } } if (!hasWinner) { // 若无符合条件的赢家 cout << "Keep going..."; } return 0; } ``` 此代码片段展示了如何通过`map`容器来管理参与者的状态,并依据给定条件选出幸运用户。 #### 另一可能性:素数对猜想 倘若PAT乙级1123题更接近于素数对猜想类型的题目(见引用[2]),那么解决方法主要围绕质数判定展开。下面是一个简化版的素数检测程序: ```cpp #include <iostream> #include <cmath> using namespace std; bool isPrime(int num){ if(num<2)return false; int sqrtNum=sqrt((double)num); for(int i=2;i<=sqrtNum;i++){ if(num%i==0)return false; } return true; } int main(){ int upperLimit,count=0,lastPrime=-2; cin>>upperLimit; for(int candidate=2;candidate<=upperLimit;candidate++){ if(isPrime(candidate)){ if(lastPrime!=-2&&candidate-lastPrime==2){ count++; } lastPrime=candidate; } } cout<<count; return 0; } ``` 上述例子说明了怎样利用循环遍历区间内的整数,逐一验证它们是否属于孪生素数之一。 由于缺乏确切描述,以上仅为猜测性质的内容分享。实际解答需参照官方文档或权威资料获取精准信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值