Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change.
class Tree {
public static Node Inorder(Node root1) {
while (root1.left != null) {
root1 = root1.left;
}
return root1;
}
public static Node deleteNode(Node root, int x) {
if (root.data > x) {
root.left = deleteNode(root.left, x);
} else if (root.data < x) {
root.right = deleteNode(root.right, x);
} else {
if (root.left == null && root.right == null) return null;
else if (root.left == null) return root.right;
else if (root.right == null) return root.left;
Node IS = Inorder(root.right);
root.data = IS.data;
root.right = deleteNode(root.right, IS.data);
}
return root;
}
}