import java.util.* ;
import java.io.*;
public class Solution {
public static BinaryTreeNode<Integer> bstDelete(BinaryTreeNode<Integer> root, int key) {
if (root == null)
return root;
if (key < root.data) {
root.left = bstDelete(root.left, key);
} else if (key > root.data) {
root.right = bstDelete(root.right, key);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
root.data = minValue(root.right);
root.right = bstDelete(root.right, root.data);
}
return root;
}
public static int minValue(BinaryTreeNode<Integer> root) {
int minValue = root.data;
while (root.left != null) {
minValue = Math.min(minValue, root.left.data);
root = root.left;
}
return minValue;
}
}








