Tried to do deletion in a bst in a different way

code is:

i sorted the bst in ascending order.
then deleted those elements which needed to be deleted…then arranged the tree in bst form (using the formation of a bst by division from the middle)and printed the preorder…it is giving the correct answer for the given input but showing wrong answer for all other cases…it should have given TLE but it is giving wrong answer…can anyone please help out in finding error in this logic?

Say input given to us is
3
2 1 3
1
2

Now tree formed without deletion should be
…2
1…3

and after deletion
…3
1
preorder: 3 1

But in your case
u first sort i.e 1 2 3
then delete 1 3 and then form tree
…1
…3
Now your preorder is 1 3

Now you see what you are doing wrong.
Now stick to original implementation ,I am not saying your code is wrong but BST formed in your case will be different from the one which is formed from the solution code.

1 Like

Have you found the mistake>
Its on line 52 if(x<=root->data)
Remove equality from here.

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.