class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public Node Insert(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = Insert(root.left, val); else root.right = Insert(root.right, val); return root; } public Node Delete(Node root, int val) { if (root == null) return null; if (val < root.val) root.left = Delete(root.left, val); else if (val > root.val) root.right = Delete(root.right, val); else { if (root.left == null && root.right == null) return null; if (root.left == null) return root.right; if (root.right == null) return root.left; Node minRight = FindMin(root.right); root.val = minRight.val; root.right = Delete(root.right, minRight.val); } return root; } private Node FindMin(Node root) { while (root.left != null) root = root.left; return root; } public void PrintInOrder(Node root) { if (root == null) return; PrintInOrder(root.left); System.Console.Write(root.val + " -> "); PrintInOrder(root.right); } } var bst = new BST(); bst.root = bst.Insert(bst.root, 5); bst.root = bst.Insert(bst.root, 3); bst.root = bst.Insert(bst.root, 7); bst.root = bst.Insert(bst.root, 2); bst.root = bst.Insert(bst.root, 4); bst.root = bst.Insert(bst.root, 6); bst.root = bst.Insert(bst.root, 8); bst.root = bst.Delete(bst.root, 3); bst.PrintInOrder(bst.root); System.Console.Write("null");
Node 3 is a leaf node (no children). Deleting it removes it directly. The in-order traversal prints nodes in ascending order, so 3 is missing.
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public Node Insert(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = Insert(root.left, val); else root.right = Insert(root.right, val); return root; } public Node Delete(Node root, int val) { if (root == null) return null; if (val < root.val) root.left = Delete(root.left, val); else if (val > root.val) root.right = Delete(root.right, val); else { if (root.left == null && root.right == null) return null; if (root.left == null) return root.right; if (root.right == null) return root.left; Node minRight = FindMin(root.right); root.val = minRight.val; root.right = Delete(root.right, minRight.val); } return root; } private Node FindMin(Node root) { while (root.left != null) root = root.left; return root; } public void PrintInOrder(Node root) { if (root == null) return; PrintInOrder(root.left); System.Console.Write(root.val + " -> "); PrintInOrder(root.right); } } var bst = new BST(); bst.root = bst.Insert(bst.root, 5); bst.root = bst.Insert(bst.root, 3); bst.root = bst.Insert(bst.root, 7); bst.root = bst.Insert(bst.root, 2); bst.root = bst.Insert(bst.root, 4); bst.root = bst.Insert(bst.root, 6); bst.root = bst.Delete(bst.root, 7); bst.PrintInOrder(bst.root); System.Console.Write("null");
Node 7 has one child (6). Deleting 7 replaces it with 6. The in-order traversal prints nodes in ascending order without 7.
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public Node Insert(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = Insert(root.left, val); else root.right = Insert(root.right, val); return root; } public Node Delete(Node root, int val) { if (root == null) return null; if (val < root.val) root.left = Delete(root.left, val); else if (val > root.val) root.right = Delete(root.right, val); else { if (root.left == null && root.right == null) return null; if (root.left == null) return root.right; if (root.right == null) return root.left; Node minRight = FindMin(root.right); root.val = minRight.val; root.right = Delete(root.right, minRight.val); } return root; } private Node FindMin(Node root) { while (root.left != null) root = root.left; return root; } public void PrintInOrder(Node root) { if (root == null) return; PrintInOrder(root.left); System.Console.Write(root.val + " -> "); PrintInOrder(root.right); } } var bst = new BST(); bst.root = bst.Insert(bst.root, 5); bst.root = bst.Insert(bst.root, 3); bst.root = bst.Insert(bst.root, 7); bst.root = bst.Insert(bst.root, 2); bst.root = bst.Insert(bst.root, 4); bst.root = bst.Insert(bst.root, 6); bst.root = bst.Insert(bst.root, 8); bst.root = bst.Delete(bst.root, 5); bst.PrintInOrder(bst.root); System.Console.Write("null");
Node 5 has two children. It is replaced by the smallest node in its right subtree, which is 6. Then 6 is deleted from its original position. The in-order traversal prints nodes in ascending order without 5.
public Node Delete(Node root, int val) { if (root == null) return null; if (val < root.val) root.left = Delete(root.left, val); else if (val > root.val) root.right = Delete(root.right, val); else { if (root.left == null && root.right == null) return null; if (root.left == null) return root.right; if (root.right == null) return root.left; Node minRight = FindMin(root.right); root.val = minRight.val; root.right = Delete(root.right, root.val); } return root; } private Node FindMin(Node root) { while (root.left != null) root = root.left; return root; }
The recursive call uses root.val instead of minRight.val after replacement, causing infinite recursion because root.val was updated but the call deletes the same value again.
Initially, there are 9 nodes. Deleting 5 and 15 removes two nodes. Both have children, but deletion replaces them with appropriate nodes, so total nodes decrease by 2.
