Ranking competition topic record (2)

text

L2-004 Is this a binary search tree?

Topic description

A binary search tree can be defined recursively as a binary tree with the following properties: For any node,

  • The key value of all nodes in its left subtree is less than the key value of this node;
  • The key value of all nodes in its right subtree is greater than or equal to the key value of this node;
  • Its left and right subtrees are binary search trees.

The so-called "mirror" of a binary search tree is a tree obtained by reversing the positions of the left and right subtrees of all nodes.

Given a sequence of integer keys, please write a program to determine whether this is the result of a preorder traversal of a binary search tree or its mirror image.

input Output

Input and output format

Input format:
The first line of input gives a positive integer N (≤1000). The next line gives N integer key values ​​separated by spaces.

Output format:
If the input sequence is the result of preorder traversal of a binary search tree or its mirror image, first output YES in one line, and then output the result of postorder traversal of the tree in the next line. There should be 1 space between numbers, and there should be no extra spaces at the beginning and end of a line. If the answer is no, output NO.

Input and output example

输入样例 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

ideas and code

First, we explain the mirrored binary search tree. In fact, even if the definition of binary search tree is changed: all nodes in the left subtree have values ​​greater than or equal to the value of the root node, and all node values ​​in the right subtree are less than the value of the root node, and the left and right subtrees are also binary search trees.

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll maxn=1e5+10;
ll a[maxn];
vector<ll> g;
ll flag=0;
ll n;
void getg(ll l,ll r){
    //cout<<l<<" "<<r<<endl;
    if(l>r)return ;
    ll l1=l+1,r1=r;
    if(!flag){
        while(l1<=r&&a[l1]<a[l])l1++;
        while(r1>l&&a[r1]>=a[l])r1--;
    }else{
        while(l1<=r&&a[l1]>=a[l])l1++;
        while(r1>l&&a[r1]<a[l])r1--;
    }
    if(l1-r1!=1)return ;//代表不是前缀序列
    getg(l+1,r1);
    getg(l1,r);
    g.push_back(a[l]);
    return ;

}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    getg(1,n);
    if(g.size()!=n){
        flag=1;
        g.clear();
        getg(1,n);
    }
    if(g.size()==n){
        cout<<"YES"<<endl;
        for(int i=0;i<n-1;i++){
            cout<<g[i]<<" ";
        }
        cout<<g[n-1]<<endl;
    }else{
        cout<<"NO"<<endl;
    }
    return 0;
}

Epilogue


"If you are undecided, you can ask the spring breeze, and if the spring breeze does not speak, you will follow your heart" means: if you are hesitant about something, ask the spring breeze how to do it. . "When you are undecided, you can ask the spring breeze, and if the spring breeze does not speak, you will follow your heart." The sentence comes from the "Sword Comes" by the online writer "Fenghuo Opera Princes". The original text is: "If you are undecided, you can ask the spring breeze. Follow your heart".

insert image description here


Guess you like

Origin blog.csdn.net/weixin_46627433/article/details/124302482