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\n", start);
//    printf("L: %d-%d\n", start + 1, i);
//    printf("R: %d-%d\n", 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\n", root->data, lo, hi, mirror);
    if (root->data < lo || root->data >= hi) {
//        printf("fail\n");
        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("\n");
}
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\n");
        postorder(root, post);
        print(post);
        return 0;
    }

    root = build_bst(nums, 0, nums.size(), true);
    if (is_bst(root, true)) {
        printf("YES\n");
        postorder(root, post);
        print(post);
        return 0;
    } else {
        printf("NO\n");
    }
    return 0;
}

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

时间: 2024-10-14 01:01:14

PAT 1043 Is It a Binary Search Tree的相关文章

PAT 1043. Is It a Binary Search Tree (25)

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 nod

PAT 1043 Is It a Binary Search Tree[二叉树][难]

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 nod

1043. Is It a Binary Search Tree (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1043. Is It a Binary Search Tree (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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

1043 Is It a Binary Search Tree (25 分)

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 no

PAT Advanced 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 greate

1043. Is It a Binary Search Tree (25)

时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue (二叉树建立方法) 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 th

1043. Is It a Binary Search Tree

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 greate

PAT Advanced Level 1064 Complete Binary Search Tree (30)(30 分)

1064 Complete Binary Search Tree (30)(30 分) 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

PAT 甲级 1099 Build A Binary Search Tree

https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 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 th