• 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;
    }
    



  • 相关阅读:
    let与const的区别
    IOS客户端UIwebview下web页面闪屏问题
    移动端click事件延迟300ms问题
    css3+visbibilty解决淡入淡出问题
    git学习之branch分支
    git学习之冲突解决办法
    webpack+vue-cli项目打包技巧
    一个高级PHP工程师所应该具备的
    多站点
    PHP error_reporting() 错误控制函数功能详解
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154142.html
Copyright © 2020-2023  润新知