Segmentation fault

If root->data < d, then you should search in the right subtree because you want to search for a value greater than root->data.
Also, statements like line 35 should be: root = delete_in_BST…


still not working

Check how you are giving input. The first value is the number of test cases and so on…
Also, it should be root=delete_in_BST(root->right,d); and not root->right=delete_in_BST(root->right,d);
Similar is the case for left call.


only 1 test case is passing

i dont know why my order is coming out to be wrong

Your delete function seems fine. Maybe you are building the tree the wrong way.

node *insertinBST(node *root,int a)
{
    if(root==NULL)
    {
        root = new node(a);
        return root;
    }

    if(a < root->data)
        root->left = insertinBST(root->left,a);
    else
        root->right = insertinBST(root->right,a);

    return root;

}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,a;
        cin>>n;
        node *root = NULL;
        for(int i=0; i<n; i++)
        {
            cin>>a;
            root = insertinBST(root,a);
        }
        int m;
        cin>>m;
        for(int j=0; j<m; j++)
        {
            cin>>a;
            deleteinBST(root,a);
        }
        preorder(root);
    }
}

Use this.


now my order is also right but only one test case is passing

Are you getting something like this?

Put a cout<<endl; after every test case.

thank you so much, i had been stuck on this for too long

Great, it worked. Please mark your doubt as resolved now.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.