• PAT 1043 Is It a Binary Search Tree


    #include <cstdio>
    #include <climits>
    #include <cstdlib>
    #include <vector>
    
    const double LO_BOUND = -1.0 + INT_MIN;
    const double HI_BOUND = +1.0 + INT_MAX;
    
    using namespace std;
    
    class Node {
    public:
        Node* L;
        Node* R;
        int data;
        Node(int d=0, Node* l = NULL, Node* r = NULL) : L(l), R(r), data(d){}
    };
    
    
    Node* build_bst(vector<Node> &nums, int start, int end, bool mirror = false) {
        if (start >= end) return NULL;
        if (start + 1 == end) {
            nums[start].R = nums[start].L = NULL;
            return &nums[start];
        }
        Node& root = nums[start];
        int i = start + 1;
        while (i<end) {
            if (!mirror) {
                if (nums[i].data >= root.data) break;
            } else {
                if (nums[i].data < root.data) break;
            }
            i++;
        }
    //    printf("Root: %d
    ", start);
    //    printf("L: %d-%d
    ", start + 1, i);
    //    printf("R: %d-%d
    ", i, end);
        
        root.L = build_bst(nums, start + 1, i, mirror);
        root.R = build_bst(nums, i, end, mirror);
        
        return &root;
    }
    
    bool is_bst(Node* root, bool mirror = false, double lo = LO_BOUND, double hi = HI_BOUND) {
        if (root == NULL) return true;
    //    printf("check %d lo=%f hi=%f mirror=%d
    ", root->data, lo, hi, mirror);
        if (root->data < lo || root->data >= hi) {
    //        printf("fail
    ");
            return false;
        }
        Node *vL = root->L, *vR = root->R, *tmp = NULL;
        if (mirror) {
            tmp = vL;
            vL = vR;
            vR = tmp;
        }
        return is_bst(vL, mirror, lo, root->data) && is_bst(vR, mirror, root->data, hi);
    }
    
    void postorder(Node* root, vector<int> &v) {
        if (root == NULL) return;
        postorder(root->L, v);
        postorder(root->R, v);
        v.push_back(root->data);
        return;
    }
    
    void print(vector<int> &v) {
        int len = v.size();
        
        if (len < 1) return;
        printf("%d", v[0]);
        for (int i=1; i<len; i++) {
            printf(" %d", v[i]);
        }
        printf("
    ");
    }
    int main() {
        int N;
        scanf("%d", &N);
        vector<Node> nums(N);
        
        for (int i=0; i<N; i++) {
            scanf("%d", &(nums[i].data));
        }
        vector<int> post;
        
        Node* root = build_bst(nums, 0, nums.size());
        if (is_bst(root)) {
            printf("YES
    ");
            postorder(root, post);
            print(post);
            return 0;
        }
    
        root = build_bst(nums, 0, nums.size(), true);
        if (is_bst(root, true)) {
            printf("YES
    ");
            postorder(root, post);
            print(post);
            return 0;
        } else {
            printf("NO
    ");
        }
        return 0;
    }

    就这样吧,还有那么多题拙计啊

  • 相关阅读:
    天使投资人如何评估创业公司价值
    web报表工具finereport常用函数的用法总结(数组函数)
    采用UltraISO制作U盘启动盘
    web报表工具FineReport常用函数的用法总结(报表函数)
    web报表工具FineReport使用中遇到的常见报错及解决办法(三)
    Dll的编写 在unity中加载
    Unity 实现模拟按键
    web报表工具FineReport最经常用到部分函数详解
    Unity UGUI
    带有天气预报的高大上web报表制作分享
  • 原文地址:https://www.cnblogs.com/lailailai/p/4090997.html
Copyright © 2020-2023  润新知