1043 Is It a Binary Search Tree (25)(25 分)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

7
8 6 5 7 10 8 11

Sample Output 1:

YES
5 7 6 8 11 10 8

Sample Input 2:

7
8 10 11 8 6 7 5

Sample Output 2:

YES
11 8 10 7 5 6 8

Sample Input 3:

7
8 6 8 5 10 9 11

Sample Output 3:

NO

题意:给出一组原始数据,按照左子树小于根节点和右子树≥根节点的规则建造一颗二叉搜索树,问原始序列是否是该树的先序遍历或者镜像树的先序遍历,如果是的话,输出YES并且输出其对应的后序遍历;如果不是输出NO

笔记:如果用数组来对原始数据和遍历结果进行存储的话,判断的时候会有一个循环,事实上这个如果将原始数据和遍历数据设置成vector类型的话,可以省略掉for循环

#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <stdio.h>
#include <math.h>
using namespace std;
const int maxn = 1050;
vector<int> pre,prem,post,postm,org;
int n;
bool flag = false;
struct Node
{
    int data;
    Node * left;
    Node * right;
}*root;
Node * newNode(int data)
{
    Node * root = new Node;
    root->data = data;
    root->left = NULL;
    root->right = NULL;
    return root;
}
void insertNode(Node *&root,int data)
{
    if(root == NULL)
    {
        root = newNode(data);
        return ;
    }
    if(root->data > data)
    {
        insertNode(root->left,data);
    }else
    {
        insertNode(root->right,data);
    }
}
Node* create()
{
    Node *root = NULL;
    for(int i=0; i<n; i++)
    {
        insertNode(root,org[i]);
    }
    return root;
}
void preOrder(Node * root)
{
    if(root==NULL)
        return;
    pre.push_back(root->data);
    preOrder(root->left);
    preOrder(root->right);
}
void postOrder(Node * root)
{
    if(root == NULL)
        return ;
    postOrder(root->left);
    postOrder(root->right);
    post.push_back(root->data);
}
void premirror(Node * root)
{
    if(root == NULL)
        return ;
    prem.push_back(root->data);
    premirror(root->right);
    premirror(root->left);
}
void postmirrot(Node * root)
{
    if(root == NULL)
        return;
    postmirrot(root->right);
    postmirrot(root->left);
    postm.push_back(root->data);
}
int main()
{
    cin>>n; int num;
    for(int i=0; i<n; i++)
    {
        cin>>num;
        org.push_back(num);
    }
    root = create();
    preOrder(root);
    postOrder(root);
    premirror(root);
    postmirrot(root);
    if(pre == org)
    {
        cout<<"YES"<<endl;
        for(int i=0; i<n; i++)
        {
            cout<<post[i];
            if(i < n-1)
                cout<<" ";
        }
        return 0;
    }
    if(prem == org)
    {
        cout<<"YES"<<endl;
        for(int i=0; i<n; i++)
        {
            cout<<postm[i];
            if(i < n-1)
                cout<<" ";
        }
        return 0;
    }
    cout<<"NO"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38851184/article/details/81331865
今日推荐