Delete Nodes From BST

Why is it failing the test cases?

deletion logic and code is correct
problem is with building the tree

you are trying to make balanced bst
which is not directed in question

you have to simply add each element to bst one by one

like this

node* insertnode(int d, node*startnode) {
		if (startnode == NULL) {
			node*nn = new node(d);
			return nn;
		}

		if (startnode->data > d) {
			startnode->left = insertnode(d, startnode->left);
		}

		else {
			startnode->right = insertnode(d, startnode->right);
		}
		return startnode;
	}
	void BuildTree(int *arr, int n) {
		for (int i = 0; i < n; i++) {
			rootnode = insertnode(arr[i], rootnode);
		}
	}

if you have more doubts regarding this feel free to ask
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved :grinning:

1 Like

only one mistake
initially root should be NULL
at line not 114
correct statemen
node *root=NULL;

Modified Code

i hope this helps
Please mark doubt as resolved :grinning: