Builidng a binary search tree from a preorder array

I don’t know why I am getting wrong output for this particular array.can somebody pls help me

    Node *constructBST(int preorder[], int *preorderIdx, int key, int min, int max, int n)
    {
        if (*(preorderIdx) >= n)
        {
            return NULL;
        }
        Node *root = NULL;
        if (key > min && key < max)
        {
            root = new Node(key);
            *preorderIdx = *preorderIdx + 1;
            if (*preorderIdx < n)
            {
                root->left = constructBST(preorder, preorderIdx, preorder[*preorderIdx], min, key, n);
            }
            if (*preorderIdx < n)
            {
                root->right = constructBST(preorder, preorderIdx, preorder[*preorderIdx], key, max, n);
            }
        }
        return root;
    }
    void printPreorder(Node *root)
    {
        if (root == NULL)
            return;
        cout << root->data << " ";
        printPreorder(root->left);
        printPreorder(root->right);
    }
    int main()
    {
        int preorder[] = {5, 1, 8, 10, 12, 2};
        int n = 5;
        int preorderIdx = 0;
        Node *root = constructBST(preorder, &preorderIdx, preorder[0], INT_MIN, INT_MAX, n);
        printPreorder(root);
        return 0;
    }