zoukankan      html  css  js  c++  java
  • 1043. Is It a Binary Search Tree (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
    

    这道题目的叙述似乎有一定问题,当判断出题目给定序列为镜像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;
    }
    



  • 相关阅读:
    Linux系统目录结构介绍
    【Android开发学习笔记】【第二课】Activity学习
    【Android开发学习笔记】【第一课】初识New Project,工程文件介绍
    Android 环境快速搭建-详细步骤-win7-64bit
    【VC6】【集成工具】将输入信息集成到VC工具中
    Intellij IDEA快捷键
    C#+Winform开发窗体程序
    使用U盘重装系统
    C#进阶学习笔记
    C#基础学习笔记
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154142.html
Copyright © 2011-2022 走看看