This is my code . Can you tell me where did I go wrong ? All the test cases are failing .
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in) ;
int t = scan.nextInt() ;
while(t-- > 0){
int n = scan.nextInt() ;
int[] arr = new int[n] ;
for(int i = 0 ; i < n ; i ++){
arr[i] = scan.nextInt() ;
}
Arrays.sort(arr) ;
BST bst = new BST(arr) ;
int m = scan.nextInt() ;
int[] del = new int[m] ;
for(int i = 0 ; i < m ; i ++){
del[i] = scan.nextInt() ;
}
for(int i = 0 ; i < del.length ; i ++){
bst.remove(del[i]) ;
}
bst.preOrder() ;
System.out.println() ;
}
}
}
class BST{
private class Node{
int data ;
Node left ;
Node right ;
}
Node root ;
BST(int[] arr){
root = construct(arr,0,arr.length-1) ;
}
private Node construct(int[] arr, int low , int high){
if(low > high){
return null ;
}
int mid = (low+high)/2 ;
Node nn = new Node() ;
nn.data = arr[mid] ;
nn.left = construct(arr,low,mid-1) ;
nn.right = construct(arr,mid+1,high) ;
return nn ;
}
public void remove(int item){
remove(root,null,item,"") ;
}
private void remove(Node root , Node parent , int item ,String child){
if(root == null){
return ;
}
if(item > root.data){
remove(root.right,root,item,"right") ;
}
else if(item < root.data){
remove(root.left,root,item,"left") ;
}
else{
if(root.left == null && root.right == null){
if(child.equals("right")){
parent.right = null ;
}
else{
parent.left = null ;
}
}
else if(root.left == null && root.right != null){
if(child.equals("right")){
parent.right = root.right ;
}
else{
parent.left = root.right ;
}
}
else if(root.left != null && root.right == null){
if(child.equals("right")){
parent.right = root.left ;
}
else{
parent.left = root.left ;
}
}
else{
int min = min(root.right) ;
root.data = min ;
remove(root.right,root,min,"right") ;
}
}
}
private int min(Node root){
if(root == null){
return 0 ;
}
if(root.left == null){
return root.data ;
}
return min(root.left) ;
}
public void preOrder(){
preOrder(root) ;
}
private void preOrder(Node root){
if(root == null){
return ;
}
System.out.print(root.data+" ") ;
preOrder(root.left) ;
preOrder(root.right) ;
}
}