What is the error in my code?

import java.util.Scanner;
class Main{

static class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left = right = null;
}
}
static Node root = null;

public static Node constructTree(Node node, int val){
if(node == null){
Node root = new Node(val);
return root;
}

if(val > node.data){
    node.right = constructTree(node.right,val);
}

if(val < node.data){
    node.left = constructTree(node.left,val);
}

return node;

}

public static void deleteNode(Node node, int val, Node parent, boolean ilc){
if(node == null) return;

if(val > node.data){
    deleteNode(node.right,val,node,false);
}else if(val < node.data){
    deleteNode(node.left, val,node,true);
}else{
    if(node.left == null && node.right == null){
        if(ilc){
            parent.left = null;
        }else{
            parent.right = null;
        }
    }else if(node.left != null && node.right == null){
        if(ilc){
            parent.left = node.left;
            node.left = null;
        }else{
            parent.right = node.left;
            node.left = null;
        }

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

    }else{
        int rmin = min(node.right);
        node.data = rmin;
        deleteNode(node.right,rmin,node,false);
    }
}

}

public static int min(Node node){
if(node == null) return Integer.MAX_VALUE;

while(node.left!=null){
    node = node.left;
}

return node.data;

}

public static void preOrder(Node node){
if(node == null) return;

System.out.print(node.data+" ");
preOrder(node.left);
preOrder(node.right);

}

public static void solve(){
int n1 = scn.nextInt();

for(int i = 0; i< n1; i++){
   root = constructTree(root,scn.nextInt());
}

int n2 = scn.nextInt();

for(int i = 0; i< n2; i++){
    deleteNode(root,scn.nextInt(),null,false);
}

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

}

public static Scanner scn = new Scanner(System.in);
public static void main(String[] args){
    int t = scn.nextInt();
    while(t-->0){
        solve();
    }
}

}

Anybody? Please . It’s passing for only 1 test case

hey @ap8730390
try and dry run your code for
1
18
172 468 963 94 951 803 683 630 198 672 327 216 451 738 798 251 558 159
11
683 159 327 94 451 738 798 172 468 963 738
You are getting null pointer exceptions

Okay I got the error but please help me rectify it . How do i put that condition?

too simple test Case
try and dry run your code for
1
1
10
1
10

Still Not Working :\

It’s giving wrong output as well :frowning:

I guess I will have to delete the whole tree.

@ap8730390
public void deleteKey(int key) {
root = deleteRec(root, key);
}

private Node deleteRec(Node root, int key) {

// / Base Case: If the tree is empty /
if (root == null)
return root;
// / Otherwise, recur down the tree */
if (key < root.data)
root.left = deleteRec(root.left, key);
else if (key > root.data)
root.right = deleteRec(root.right, key);
// if key is same as root’s key, then This is the node
// to be deleted
else {
// node with only one child or no child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// node with two children: Get the inorder successor (smallest
// in the right subtree)
root.data = min(root.right);
// Delete the inorder successor
root.right = deleteRec(root.right, root.data);
}
return root;
}

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.

This time it passed for two test cases but still showing error

@ap8730390
Send me your submitted code

import java.util.Scanner; class Bst{ class Node{ int data; Node left; Node right; Node(int data){ this.data = data; left = right = null; } } Node root = null; public Node constructTree(Node node, int val){ if(node == null){ Node root = new Node(val); return root; } if(val > node.data){ node.right = constructTree(node.right,val); } if(val < node.data){ node.left = constructTree(node.left,val); } return node; } public Node deleteNode(Node node, int val){ if(node == null) return null; if(val > node.data){ node.right = deleteNode(node.right,val); }else if(val < node.data){ node.left = deleteNode(node.left, val); }else{ if(node.left == null) {return node.right; } else if(node.right==null){ return node.left; }else{ int rmin = min(node.right); node.data = rmin; node.right = deleteNode(node.right,rmin); } } return node; } public int min(Node node){ if(node == null) return Integer.MAX_VALUE; while(node.left!=null){ node = node.left; } return node.data; } public void preOrder(Node node){ if(node == null) return; System.out.print(node.data+" "); preOrder(node.left); preOrder(node.right); } } class Main{ public static void solve(){ int n1 = scn.nextInt(); Bst bst = new Bst(); for(int i = 0; i< n1; i++){ bst.root = bst.constructTree(bst.root,scn.nextInt()); } int n2 = scn.nextInt(); for(int i = 0; i< n2; i++){ bst.deleteNode(bst.root,scn.nextInt()); } bst.preOrder(bst.root); System.out.println(); } public static Scanner scn = new Scanner(System.in); public static void main(String[] args){ int t = scn.nextInt(); while(t–>0){ solve(); } } }\

Here is my submitted code

import java.util.Scanner; class Bst{ class Node{ int data; Node left; Node right; Node(int data){ this.data = data; left = right = null; } } Node root = null; public Node constructTree(Node node, int val){ if(node == null){ Node root = new Node(val); return root; } if(val > node.data){ node.right = constructTree(node.right,val); } if(val < node.data){ node.left = constructTree(node.left,val); } return node; } public Node deleteNode(Node node, int val){ if(node == null) return null; if(val > node.data){ node.right = deleteNode(node.right,val); }else if(val < node.data){ node.left = deleteNode(node.left, val); }else{ if(node.left == null) {return node.right; } else if(node.right==null){ return node.left; }else{ int rmin = min(node.right); node.data = rmin; node.right = deleteNode(node.right,rmin); } } return node; } public int min(Node node){ if(node == null) return Integer.MAX_VALUE; while(node.left!=null){ node = node.left; } return node.data; } public void preOrder(Node node){ if(node == null) return; System.out.print(node.data+" "); preOrder(node.left); preOrder(node.right); } } class Main{ public static void solve(){ int n1 = scn.nextInt(); Bst bst = new Bst(); for(int i = 0; i< n1; i++){ bst.root = bst.constructTree(bst.root,scn.nextInt()); } int n2 = scn.nextInt(); for(int i = 0; i< n2; i++){ bst.deleteNode(bst.root,scn.nextInt()); } bst.preOrder(bst.root); System.out.println(); } public static Scanner scn = new Scanner(System.in); public static void main(String[] args){ int t = scn.nextInt(); while(t–>0){ solve(); } } }

1
18
172 468 963 94 951 803 683 630 198 672 327 216 451 738 798 251 558 159
11
683 159 327 94 451 738 798 172 468 963 738
your code given : 172 558 198 216 251 951 803 630 672
its correct output is 558 198 216 251 951 803 630 672


You can see my code