L2-004 这是二叉搜索树吗? (25 分)

53 篇文章 8 订阅
10 篇文章 0 订阅

一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点,

  • 其左子树中所有结点的键值小于该结点的键值;
  • 其右子树中所有结点的键值大于等于该结点的键值;
  • 其左右子树都是二叉搜索树。
    所谓二叉搜索树的“镜像”,即将所有结点的左右子树对换位置后所得到的树。

给定一个整数键值序列,现请你编写程序,判断这是否是对一棵二叉搜索树或其镜像进行前序遍历的结果。

输入格式:

输入的第一行给出正整数 N(≤1000)。随后一行给出 N 个整数键值,其间以空格分隔。

输出格式:

如果输入序列是对一棵二叉搜索树或其镜像进行前序遍历的结果,则首先在一行中输出 YES ,然后在下一行输出该树后序遍历的结果。数字间有 1 个空格,一行的首尾不得有多余空格。若答案是否,则输出 NO。

输入样例 1:

7
8 6 5 7 10 8 11

输出样例 1:

YES
5 7 6 8 11 10 8

输入样例 2:

7
8 10 11 8 6 7 5

输出样例 2:

YES
11 8 10 7 5 6 8

输入样例 3:

7
8 6 8 5 10 9 11

输出样例 3:

NO

答案:

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-8
#define x first
#define y second
const int mod = 1e9 + 7;
const int MOD = 1e4+7;
const int N = 1e5 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
int dxy[][2]={{0,1},{1,0},{1,1},{-1,1}};
using namespace std;

int dp[M];
int tot=0;

bool judge(int l,int r){
    int k=l+1;
    int tot=0;
    if(l>=r) return 1;
    rep(i,l+1,r){
        if(dp[i]>=dp[l]&&!tot){
            k=i;
            tot++;
        }
        else if(tot&&dp[i]<dp[l]) return 0;
    }
    if(judge(l+1,k-1)&&judge(k,r)) return 1;
    else return 0;
}

void post(int l,int r){
    int k=l+1;
    if(l>r) return ;
    else if(l==r){
        if(!tot){
            cout<<dp[l];
            tot++;
        }
        else cout<<" "<<dp[l];
        return ;
    }
    rep(i,l+1,r){
        if(dp[i]>=dp[l]){
            k=i;
            break;
        }
    }
    post(l+1,k-1);
    post(k,r);
    cout<<" "<<dp[l];
    return ;
}

bool check(int l,int r){
    int k=l+1;
    int tot=0;
    if(l>=r) return 1;
    rep(i,l+1,r){
        if(dp[i]<dp[l]&&!tot){
            k=i;
            tot++;
        }
        else if(tot&&dp[i]>=dp[l]) return 0;
    }
    if(check(l+1,k-1)&&check(k,r)) return 1;
    else return 0;
}

void dis(int l,int r){
    int k=l+1;
    if(l>r) return ;
    else if(l==r){
        if(!tot){
            cout<<dp[l];
            tot++;
        }
        else cout<<" "<<dp[l];
        return ;
    }
    rep(i,l+1,r){
        if(dp[i]<dp[l]){
            k=i;
            break;
        }
    }
    dis(l+1,k-1);
    dis(k,r);
    cout<<" "<<dp[l];
    return ;
}

void solve(){
    int n;
    cin>>n;
    rep(i,1,n) cin>>dp[i];
//    bool flag=0;
    if(dp[1]>dp[2]){
        bool flag=judge(1,n);
        if(flag){
           cout<<"YES"<<endl;
            post(1,n);
            cout<<endl;
        }
        else cout<<"NO"<<endl;
    }
    else {
        bool flag=check(1,n);
        if(flag){
           cout<<"YES"<<endl;
            dis(1,n);
            cout<<endl;
        }
        else cout<<"NO"<<endl;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值