码迷,mamicode.com
首页 > 其他好文 > 详细

1043. Is It a Binary Search Tree (25)

时间:2015-07-12 15:49:25      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:pat   c++   二叉搜索树   bst   镜像   

题目如下:

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

这道题目的叙述似乎有一定问题,当判断出题目给定序列为镜像BST的前序序列时,应当输出镜像BST的后序序列。

第一步是建立BST,建立方法很简单,首先定义一个Node为空来存储根结点,定义一个insert方法,当T为空时建立该结点,当T不空时,根据BST条件对T->left和T->right进行递归。

void insert(Node &T,int num){
    if(!T){
        T = new TreeNode();
        T->data = num;
        return;
    }

    if(num < T->data){
        insert(T->left,num);
    }else{
        insert(T->right,num);
    }
}
需要注意的是Node参数应当传入引用,因此加一个&,否则无法直接操作到外部的T参数。

对于整个输入序列,调用N次insert方法即可得到完整的BST,且根结点为T。


下面只需要对BST进行一次前序遍历和一次镜像前序遍历(根右左),看题目给出的序列和哪个相符,可以判断出是BST还是MBST,如果都不是则输出NO。

完整代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

typedef struct TreeNode* Node;
struct TreeNode{
    Node left;
    Node right;
    int data;

    TreeNode(){
        left = right = NULL;
    }
};

int *pre;
int N;
vector<int> preOrderV;
vector<int> mpreOrderV;
vector<int> result;

void insert(Node &T,int num){
    if(!T){
        T = new TreeNode();
        T->data = num;
        return;
    }

    if(num < T->data){
        insert(T->left,num);
    }else{
        insert(T->right,num);
    }
}

void preOrder(Node T){
    if(!T) return;
    preOrderV.push_back(T->data);
    preOrder(T->left);
    preOrder(T->right);

}

void postOrder(Node T){
    if(!T) return;
    postOrder(T->left);
    postOrder(T->right);
    result.push_back(T->data);
}

void MBSTpreOrder(Node T){
    if(!T) return;
    mpreOrderV.push_back(T->data);
    MBSTpreOrder(T->right);
    MBSTpreOrder(T->left);
}

bool arrayEqual2Vector(int *arr, vector<int> vec){
    for(int i = 0; i < vec.size(); i++){
        if(arr[i] != vec[i]) return false;
    }
    return true;
}

void mirror(Node T){
    if(!T)return;
    Node temp = T->left;
    T->left = T->right;
    T->right = temp;
    mirror(T->left);
    mirror(T->right);
}

int main()
{
    cin >> N;
    pre = (int*)malloc(sizeof(int)*N);
    int num;
    Node T = NULL;
    for(int i = 0; i < N; i++){
        scanf("%d",&num);
        pre[i] = num;
        insert(T,num);
    }
    preOrderV.clear();
    mpreOrderV.clear();
    result.clear();

    preOrder(T);
    MBSTpreOrder(T);

    if(arrayEqual2Vector(pre,preOrderV)){
        cout << "YES" << endl;
        postOrder(T);
    }else if(arrayEqual2Vector(pre,mpreOrderV)){
        cout << "YES" << endl;
        mirror(T);
        postOrder(T);
    }else{
        cout << "NO" << endl;
    }

    if(result.size() != 0){
        cout << result[0];
        for(int i = 1; i < result.size(); i++)
            printf(" %d",result[i]);
        cout << endl;
    }

    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

1043. Is It a Binary Search Tree (25)

标签:pat   c++   二叉搜索树   bst   镜像   

原文地址:http://blog.csdn.net/xyt8023y/article/details/46849933

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!