L2-004. 这是二叉搜索树吗?

给一颗二叉搜索树的前序遍历,问你这是一颗二叉搜索树吗。

在创建的时候,一旦往树的右边创建,就不能往左边走了,否者就GG。


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
using namespace std;
struct node{
    int x;
    bool l,r;
    struct node *left;
    struct node *right;
};
int p[1005];
int l,r;
int flag;
struct node *newtree(struct node *t,int a){
    struct node *p,*q;
    p=t;
    if(l==1){
        while(p!=NULL){
        if(a>=p->x){
                p->l=0;
                q=p;
            p=p->right;

        }
        else{

            if(p->l==1){
                q=p;
            p=p->left;
            }
            else{
                flag=1;
                break;
            }
        }
    }}
    else if(l==0)
    while(p!=NULL){
        if(a>=p->x){
               // cout<<p->r<<" "<<p->x<<" "<<a<<endl;
                if(p->r==1){
                     q=p;
            p=p->right;
                }
              else{
                flag=1;
                break;
              }

        }
        else{
            p->r=0;
            q=p;
            //cout<<p->x<<" "<<p->r<<endl;
            p=p->left;

        }
    }
    p=(struct node *)malloc(sizeof(struct node));
        p->x=a;
       // cout<<p->x<<endl;
        if(a>=q->x){
            q->right=p;
        }
        else q->left=p;
        p->l=1;
        p->r=1;
        p->left=NULL;
        p->right=NULL;
    return t;
}
void houxu(node *t){
    node *p;
    p=t;
    if(p==NULL) return ;
    houxu(p->left);
    houxu(p->right);
    if(flag==0){
       cout<<p->x;
       flag=1;
    }
    else{
        cout<<" "<<p->x;
    }

}
void houxu1(node *t){
    node *p;
    p=t;
    if(p==NULL) return ;
    houxu1(p->right);
    houxu1(p->left);
    if(flag==0){
       cout<<p->x;
       flag=1;
    }
    else{
        cout<<" "<<p->x;
    }

}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        node *tree;
        flag=0;
        for(int i=0;i<n;i++){
            scanf("%d",&p[i]);
        }
        tree=(struct node *)malloc(sizeof(struct node));
        tree->x=p[0];
       // cout<<tree->x<<endl;
        if(p[1]<p[0]){
            l=1;
            r=0;
        }else{
            l=0;
            r=1;
        }
        //tree=NULL;
        tree->left=NULL;
        tree->right=NULL;
        tree->l=1;
        tree->r=1;
        for(int i=1;i<n;i++){
            tree=newtree(tree,p[i]);
            //cout<<tree->x<<endl;
        }
        //cout<<tree->x<<endl;
        if(flag==0)
        {
            printf("YES\n");
            //printf("")
            if(l==1)
            houxu(tree);
            else {
                    houxu1(tree);
            }
        }
        else{
            printf("NO");
        }
        cout<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值