7-13 是否完全二叉搜索树 (30 分)

题目链接
在这里插入图片描述
在这里插入图片描述

做法一(数组模拟):

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int a[3000010];//2^20大约在1e6左右,所以再开大一点

void add(int i, int x) {//插入函数
    if (a[i] == -1) {//如果这个位置之前没有值,就直接放上去
        a[i] = x;
        return;
    }
    if (x > a[i])//如果这个值比根节点大就递归左边(题目要求)
        add(2 * i, x);
    else//否则递归右边
        add(2 * i + 1, x);
    return;
}

int main(int argc, char const *argv[]) {
    int n;
    cin >> n;
    memset(a, -1, sizeof(a));//初始化
    for (int i = 1; i <= n; i++) {//数组模拟建树
        int x;
        cin >> x;
        add(1, x);
    }
    int flag = 1;//用于判断是否是第一个输出,从而确定是否要空格
    int i = 1;//遍历
    int ans = 1;//标记是否是完全二叉树
    int sum = 0;//输出元素的个数
    while (1) {
        while (a[i] == -1) i++;
        if (flag)
            printf("%d", a[i]);
        else
            printf(" %d", a[i]);
        flag = 0;
        i++;
        sum++;//输出了一次
        if (sum == n) break;//全部输出完毕就跳出循环
    }
    printf("\n");
    for (int i = 1; i <= n; i++)//检查是否是完全二叉树
        if (a[i] == -1) {
            ans = 0;
            break;
        }
    if (ans)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

做法二(常规建树):

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;

struct node {
    int data;
    int id;
    struct node *left;
    struct node *right;
};

struct node *creat(struct node *root, int x, int num) {//建树
    if (root == NULL) {
        root = new node;//分配空间
        root->id = num;
        root->data = x;
        root->left = NULL;
        root->right = NULL;
    } else {
        if (x > root->data)
            root->left = creat(root->left, x, num * 2);
        else
            root->right = creat(root->right, x, num * 2 + 1);
    }
    return root;
}

bool flag;
bool ans;

void print(struct node *root) {
    struct node *temp;
    queue<struct node *> q;
    q.push(root);
    while (!q.empty()) {
        temp = q.front();
        q.pop();
        if (!flag)
            printf("%d", temp->data);
        else
            printf(" %d", temp->data);
        if (temp->id > n) ans = true;//如果编号大于n即代表不是完全二叉树
        flag = true;
        if (temp->left != NULL) q.push(temp->left);
        if (temp->right != NULL) q.push(temp->right);
    }
    printf("\n");
    return;
}

int main(int argc, char const *argv[]) {
    cin >> n;
    struct node *root = NULL;//初始为NULL
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        root = creat(root, x, 1);
    }
    print(root);
    if (ans)
        printf("NO\n");
    else
        printf("YES\n");
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值