Mam Im unable to pass the test cases can u please help me out
Unable to pass test cases
import java.util.*; public class Main { private class Node { int data; Node left; Node right; } private Node root; int size; public Main(int[] arr) { this.root = construct(arr, 0, arr.length - 1); } private Node construct(int arr[], int lo, int hi) { if (lo > hi) { return null; } int mid = (lo + hi) / 2; Node nn = new Node(); nn.data = arr[mid]; nn.left = construct(arr, lo, mid - 1); nn.right = construct(arr, mid + 1, hi); return nn; } public void preorder() { this.preorder(this.root); } private void preorder(Node node) { if (node == null) { return; } System.out.print(node.data + " "); preorder(node.left); preorder(node.right); } public void remove(int item) { this.remove(this.root, null, false, item); } private void remove(Node node, Node parent, boolean ilc, int item) { if (node == null) { return; } if (item > node.data) { remove(node.right, node, false, item); } else if (item < node.data) { remove(node.left, node, true, item); } else { // case 1: 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.right; } else { parent.right = node.right; } } else if (node.left != null && node.right == null) { if (ilc) { parent.left = node.left; } else { parent.right = node.left; } } else { // n.left!=null && node.right!=null; int min = min(node.right); node.data = min; remove(node.right, node, false, min); } } } private int min(Node root) { int minv = root.data; while (root.left != null) { minv = root.left.data; root = root.left; } return minv; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t != 0) { int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int m = sc.nextInt(); int arr2[] = new int[m]; for (int i = 0; i < m; i++) { arr2[i] = sc.nextInt(); } Arrays.sort(arr); Main obj = new Main(arr); for (int i = 0; i < m; i++) { obj.remove(arr2[i]); } obj.preorder(); t–; } } }
mam please look into it.IM unable to pass the test cases.
try to debug for this input
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
ans:
558 198 216 251 951 803 630 672
The array given in the input is not the preorder traversal of the BST to be formed. It is just a random unsorted array and you need to create a BST from the given unsorted array
but mam to construct a bst we have to consider it that the array is sorted?
no see this:
for the input provided by you my code is throwing null pointer exception.Why this is happening?Also plzz help me out with the error present in my code.
mam please debug my code as im unable to find the error
okay let me go through the code once
corrected code:
always notice if there is new line for each test case .other mistake i have marked .
please mark your doubt as resolved 
Mam why always in the case of right child we are checking if(root==null)?
if(ilc){…} else{ if(root==null)…}