Delete Node from BST: Two Test Cases given run Time Error


import java.util.*;
public class Main {

//Node class
private static class Node
{
	int data;
	Node left;
	Node right;

	Node(int data, Node left, Node right)
	{
		this.data = data;
		this.left = left;
		this.right = right;
	}
}


private static Node root;



//construct BST
private static Node constructBST(int [] A, int n)
{
	Node root = null;

	for(int i = 0; i<n; i++)
		root = insert(root, A[i]);

	return root;
}



//insert
private static Node insert(Node node, int value)
{
	if(node == null)
		return new Node(value, null, null);


	if(value > node.data)
	{
		node.right = insert(node.right, value);
	}
	else   //if value <= node.data duplicacy handled
	{
		node.left = insert(node.left , value);
	}

	return node;
}





//min finder in the BST
public static int min(Node node)
{
	if(node == null)
		return Integer.MAX_VALUE;
	
	int lm = min(node.left);
	int rm = min(node.right);

	return Math.min(node.data, Math.min(lm,rm));
}




//remove function
private static void remove(Node node, Node parent, boolean ilc, int element)
{
	if(node == null)
		return;
	
	if(element < node.data)
	{
		remove(node.left, node, true, element);
	}
	else if(element > node.data)
	{
		remove(node.right, node, false, element);
	}
	//if node.data == element
	else
	{
		if(node.left == null && node.right == null)
		{
			//if root node is to be deleted and there is only one node in the BST, i.e, the root node
			if(parent == null)
				root = null;

			else if(ilc)
				parent.left = null;
			else
				parent.right = null;
		}

		else if(node.left != null && node.right == null)
		{
			if(ilc)
				parent.left = node.left;
			else
				parent.right = node.left;
		}

		else if(node.left == null && node.right != null)
		{
			if(ilc)
				parent.left = node.right;
			else
				parent.right = node.right;
		}

		else
		{
			//if node.left and node.right both are not null, replace node with min from right, or max from left, since we are given inorder successor, that means , min from right subtree and then delete the min from the right subtree if we had inOrder predecessor, then , we would take max from left
			int minVal = min(node.right);
			node.data = minVal;
			remove(node.right, node, false, minVal);
		}

	}
}





//preOrder
private static void preOrder(Node node)
{
	if(node == null)
		return;
	
	System.out.print(node.data + " ");
	preOrder(node.left);
	preOrder(node.right);
}




public static void main(String args[]) {
	
	Scanner sc = new Scanner(System.in);

	int t = sc.nextInt();

	while(t-- != 0)
	{
		//input for BST
		int n = sc.nextInt();

		int [] treeArray = new int [n];

		for(int i = 0; i<n; i++)
			treeArray[i] = sc.nextInt();

		
		//input for nodes to be deleted
		int m = sc.nextInt();

		int [] delete = new int[m];

		for(int i = 0; i<m; i++)
			delete[i] = sc.nextInt();

		
		//construct BST from the given array without sorting it
		root = constructBST(treeArray , n);

		
		//deleting the nodes
		for(int i = 0; i<m; i++)
			remove(root, null, false, delete[i]);

		//preOrder
		preOrder(root);
		
		System.out.println();

	}
}

}

@arjunsabu99 Lemme check bro!

Sir still the test cases are not passing

please help fast sir!!!

@arjunsabu99 @arjunsabu99 Bro suppose you are deleting root node. Lets say we have test case:-
1
2
6 5
1
6

Now here

It will give you a null pointer exception because you are trying to access parents right and parent of root is null!

So bro you have to handle this case!

Try it if you are not able to do it, ask me i will help you.