7-5 是否完全二叉搜索树 (30分)【数组+链表】

将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果。
输入格式:

输入第一行给出一个不超过20的正整数N;第二行给出N个互不相同的正整数,其间以空格分隔。

输出格式:

将输入的N个正整数顺序插入一个初始为空的二叉搜索树。在第一行中输出结果树的层序遍历结果,数字间以1个空格分隔,行的首尾不得有多余空格。第二行输出YES,如果该树是完全二叉树;否则输出NO。

输入样例1:

9
38 45 42 24 58 30 67 12 51
输出样例1:

38 45 24 58 42 30 12 67 51
YES
输入样例2:

8
38 24 12 45 58 67 42 51
输出样例2:

38 45 24 58 42 12 67 51
NO
ac代码(数组做法):

#include <bits/stdc++.h>

using namespace std;
int tree[10000];
int main()
{
    int n;
    cin>>n;
    int maxx=0;
    for(int i=0; i<n; i++)
    {
        int x;
        cin>>x;
        int now=1;//指针
        while(tree[now])//建树
        {
            if(x>tree[now])//左大右小
            {
                now=2*now;
            }
            else now=2*now+1;
        }
        if(now>maxx)
        {
            maxx=now;
        }
        tree[now]=x;
    }
    int flag=0;
    for(int i=1; i<=maxx; i++)
    {
        if(tree[i])
        {
            if(i!=1)
            {
                cout<<" ";
            }
            cout<<tree[i];
        }
        else flag=1;
    }
    putchar('\n');
    if(!flag)
    {
        cout<<"YES"<<endl;
    }
    else cout<<"NO"<<endl;
    return 0;
}

之前出错原因:判断是否是完全二叉树出错,不是单纯判断如果右子树存在而左子树不存在这种情况非法,而是应该判断每层如果相对位置右边有元素而左边没有非法
链表做法:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 1000000
using namespace std;
int n;
int a[21];
struct node
{
    int data;
    struct node *leftchild,*rightchild;
};
struct node *create(int key,struct node *root)
{
    if(root==NULL)
    {
        root=new node;
        root->data=key;
        root->leftchild=NULL;
        root->rightchild=NULL;
        //return root;
    }
    else
    {
        if(root->data>key)
        {
            root->rightchild=create(key,root->rightchild);
        }
        else
        {
            root->leftchild=create(key,root->leftchild);
        }
        //return root;
    }
    return root;
};
int f;
void cengxu(struct node *root)
{
    struct node *temp[10001];
    int head=0,tail=0;
    temp[tail++]=root;
    int flag=0;
    while(head<tail)
    {
        struct node *top=temp[head++];
        if(top==NULL)
        {
            if(f==-1)
            {
                f=0;
            }
            continue;
        }
        else
        {
            if(!f)
            {
                f=1;
            }
            if(!flag)
            {
                cout<<top->data;
                flag=1;
            }
            else
            {
                cout<<" "<<top->data;
            }
            temp[tail++]=top->leftchild;
            temp[tail++]=top->rightchild;
        }
    }
}

int main()
{
    scanf("%d",&n);
    struct node *root;
    root=NULL;
    for(int i=0;i<n;i++)
    {
        int key;
        cin>>key;
        root=create(key,root);
    }
    f=-1;
    cengxu(root);
    cout<<endl;
    if(f)
    {
        cout<<"NO"<<endl;
    }
    else
    {
        cout<<"YES"<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值