Bird
Raised Fist0
DSA C#programming~20 mins

BST Delete Operation in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Delete Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting a leaf node in BST
What is the printed state of the BST after deleting the leaf node with value 3?
DSA C#
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");
A2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
B2 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
C2 -> 4 -> 5 -> 7 -> 6 -> 8 -> null
D3 -> 2 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
Attempts:
2 left
💡 Hint
Deleting a leaf node removes it directly without rearranging other nodes.
❓ Predict Output
intermediate
2:00remaining
Output after deleting a node with one child in BST
What is the printed state of the BST after deleting the node with value 7 which has one child?
DSA C#
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");
A2 -> 3 -> 4 -> 5 -> 6 -> null
B2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
C2 -> 3 -> 4 -> 5 -> null
D2 -> 3 -> 4 -> 5 -> 7 -> null
Attempts:
2 left
💡 Hint
Deleting a node with one child replaces it with its child.
❓ Predict Output
advanced
2:00remaining
Output after deleting a node with two children in BST
What is the printed state of the BST after deleting the node with value 5 which has two children?
DSA C#
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");
A2 -> 3 -> 4 -> 7 -> 6 -> 8 -> null
B2 -> 3 -> 4 -> 6 -> 7 -> 8 -> 5 -> null
Cllun >- 8 >- 7 >- 6 >- 4 >- 3 >- 2
D2 -> 3 -> 4 -> 6 -> 7 -> 8 -> null
Attempts:
2 left
💡 Hint
Deleting a node with two children replaces it with the smallest node in the right subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in BST delete method
What error will this code produce when trying to delete a node from the BST?
DSA C#
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;
}
AStackOverflowException due to infinite recursion
BNullReferenceException when accessing root.left
CNo error, code runs correctly
DInvalidOperationException due to modifying tree during traversal
Attempts:
2 left
💡 Hint
Check the recursive call after replacing the node's value.
🧠 Conceptual
expert
2:00remaining
Number of nodes after multiple deletions in BST
Given a BST with nodes inserted in this order: 10, 5, 15, 3, 7, 12, 18, 6, 8. After deleting nodes 5 and 15, how many nodes remain in the BST?
A6
B8
C7
D5
Attempts:
2 left
💡 Hint
Count nodes before and after deletions carefully.

Practice

(1/5)
1. What happens when you delete a leaf node (a node with no children) from a Binary Search Tree (BST)?
easy
A. The node is replaced by its right child.
B. The entire tree is restructured from the root.
C. The node is replaced by its left child.
D. The node is simply removed without affecting other nodes.

Solution

  1. Step 1: Understand leaf node deletion

    A leaf node has no children, so removing it does not affect other nodes.
  2. Step 2: Effect on BST structure

    Since no children exist, no restructuring is needed; just remove the node.
  3. Final Answer:

    The node is simply removed without affecting other nodes. -> Option D
  4. Quick Check:

    Leaf node deletion = simple removal [OK]
Hint: Leaf nodes delete by direct removal only [OK]
Common Mistakes:
  • Thinking the tree restructures after leaf deletion
  • Replacing leaf node with a child that doesn't exist
  • Confusing leaf node deletion with internal node deletion
2. Which of the following is the correct C# syntax to delete a node with value 10 from a BST using a method named DeleteNode?
easy
A. root.DeleteNode(10);
B. root = DeleteNode(root, 10);
C. DeleteNode(root, 10);
D. DeleteNode(10, root);

Solution

  1. Step 1: Understand method signature

    Usually, BST delete methods return the new root after deletion, so assignment is needed.
  2. Step 2: Correct method call syntax

    Calling DeleteNode(root, 10) returns updated root, so root = DeleteNode(root, 10); is correct.
  3. Final Answer:

    root = DeleteNode(root, 10); -> Option B
  4. Quick Check:

    Assign returned root after deletion [OK]
Hint: Assign returned root after delete call [OK]
Common Mistakes:
  • Not assigning the returned root after deletion
  • Calling delete as instance method on root node
  • Passing arguments in wrong order
3. Given the BST below and the C# code to delete node 15, what is the in-order traversal output after deletion?
      20
     /  \
   10    30
     
     15
root = DeleteNode(root, 15);
medium
A. [10, 20, 30]
B. [10, 15, 20, 30]
C. [20, 30]
D. [10, 20, 15, 30]

Solution

  1. Step 1: Identify node to delete and its position

    Node 15 is a leaf node (no children) under 10.
  2. Step 2: Delete node 15 and perform in-order traversal

    After deleting 15, in-order traversal visits nodes in ascending order: 10, 20, 30.
  3. Final Answer:

    [10, 20, 30] -> Option A
  4. Quick Check:

    In-order after deleting leaf = sorted without deleted node [OK]
Hint: In-order skips deleted leaf node [OK]
Common Mistakes:
  • Including deleted node in traversal output
  • Misordering nodes in traversal
  • Confusing pre-order or post-order with in-order
4. Consider this C# method snippet for deleting a node from a BST:
Node DeleteNode(Node root, int key) {
  if (root == null) return root;
  if (key < root.val) root.left = DeleteNode(root.left, key);
  else if (key > root.val) root.right = DeleteNode(root.right, key);
  else {
    if (root.left == null) return root.right;
    else if (root.right == null) return root.left;
    root.val = MinValue(root.right);
    root.right = DeleteNode(root.right, root.val);
  }
  return root;
}
What is the bug in this code?
medium
A. The method does not update root.left or root.right after recursive calls.
B. The method does not handle the case when both children exist properly.
C. The method does not handle deleting leaf nodes correctly.
D. The method does not return the updated root after deletion.

Solution

  1. Step 1: Analyze deletion when both children exist

    The method correctly replaces root's value with the minimum in the right subtree and deletes that minimum node recursively.
  2. Step 2: Check correctness of recursive updates

    The method updates root.left and root.right after recursive calls, so this is handled properly.
  3. Step 3: Identify the actual bug

    There is no bug in handling both children case or returning root; the code is correct.
  4. Final Answer:

    None of the provided options correctly identify a bug. -> Option A
Hint: Check both children case carefully [OK]
Common Mistakes:
  • Thinking leaf deletion is buggy here
  • Missing recursive update of child pointers
  • Assuming method forgets to return root
5. You have a BST with nodes: 50, 30, 70, 20, 40, 60, 80. You delete node 50 (the root). Which node replaces the root to maintain BST properties?
hard
A. Node 30 (left child of root)
B. Node 40 (maximum in left subtree)
C. Node 60 (minimum in right subtree)
D. Node 70 (right child of root)

Solution

  1. Step 1: Identify node to delete and its children

    Node 50 is root with two children: left subtree (30, 20, 40) and right subtree (70, 60, 80).
  2. Step 2: Find replacement node for root deletion

    Standard BST deletion replaces node with minimum node in right subtree, which is 60.
  3. Final Answer:

    Node 60 (minimum in right subtree) -> Option C
  4. Quick Check:

    Root replaced by min right subtree node [OK]
Hint: Replace deleted node with min right subtree node [OK]
Common Mistakes:
  • Replacing with max left subtree instead of min right
  • Replacing with immediate child without checking subtree
  • Confusing replacement rules for BST deletion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard