Bird
Raised Fist0
DSA Cprogramming~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 20?
DSA C
/* BST structure before deletion:
       30
      /  \
    20    40
*/

// Delete node with value 20
// Print inorder traversal after deletion

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

struct Node* insert(struct Node* root, int data) {
    if (root == NULL) return newNode(data);
    if (data < root->data) root->left = insert(root->left, data);
    else root->right = insert(root->right, data);
    return root;
}

struct Node* minValueNode(struct Node* node) {
    struct Node* current = node;
    while (current && current->left != NULL) current = current->left;
    return current;
}

struct Node* deleteNode(struct Node* root, int key) {
    if (root == NULL) return root;
    if (key < root->data) root->left = deleteNode(root->left, key);
    else if (key > root->data) root->right = deleteNode(root->right, key);
    else {
        if (root->left == NULL) {
            struct Node* temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            struct Node* temp = root->left;
            free(root);
            return temp;
        }
        struct Node* temp = minValueNode(root->right);
        root->data = temp->data;
        root->right = deleteNode(root->right, temp->data);
    }
    return root;
}

void inorder(struct Node* root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d -> ", root->data);
        inorder(root->right);
    }
}

int main() {
    struct Node* root = NULL;
    root = insert(root, 30);
    root = insert(root, 20);
    root = insert(root, 40);

    root = deleteNode(root, 20);

    inorder(root);
    printf("null\n");
    return 0;
}
A10 -> 20 -> 30 -> 40 -> null
B10 -> 30 -> 40 -> null
C10 -> 40 -> null
D30 -> 40 -> 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 20 which has one child?
DSA C
/* BST structure before deletion:
       30
      /  \
    20    40
      
      25
*/

// Delete node with value 20
// Print inorder traversal after deletion

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

struct Node* insert(struct Node* root, int data) {
    if (root == NULL) return newNode(data);
    if (data < root->data) root->left = insert(root->left, data);
    else root->right = insert(root->right, data);
    return root;
}

struct Node* minValueNode(struct Node* node) {
    struct Node* current = node;
    while (current && current->left != NULL) current = current->left;
    return current;
}

struct Node* deleteNode(struct Node* root, int key) {
    if (root == NULL) return root;
    if (key < root->data) root->left = deleteNode(root->left, key);
    else if (key > root->data) root->right = deleteNode(root->right, key);
    else {
        if (root->left == NULL) {
            struct Node* temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            struct Node* temp = root->left;
            free(root);
            return temp;
        }
        struct Node* temp = minValueNode(root->right);
        root->data = temp->data;
        root->right = deleteNode(root->right, temp->data);
    }
    return root;
}

void inorder(struct Node* root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d -> ", root->data);
        inorder(root->right);
    }
}

int main() {
    struct Node* root = NULL;
    root = insert(root, 30);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 25);

    root = deleteNode(root, 20);

    inorder(root);
    printf("null\n");
    return 0;
}
A25 -> 40 -> null
B30 -> 40 -> null
C25 -> 30 -> 40 -> null
D20 -> 25 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
When deleting a node with one child, the child replaces the deleted node.
❓ 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 30 which has two children?
DSA C
/* BST structure before deletion:
       30
      /  \
    20    40
   /  \    \
 10   25    50
*/

// Delete node with value 30
// Print inorder traversal after deletion

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

struct Node* insert(struct Node* root, int data) {
    if (root == NULL) return newNode(data);
    if (data < root->data) root->left = insert(root->left, data);
    else root->right = insert(root->right, data);
    return root;
}

struct Node* minValueNode(struct Node* node) {
    struct Node* current = node;
    while (current && current->left != NULL) current = current->left;
    return current;
}

struct Node* deleteNode(struct Node* root, int key) {
    if (root == NULL) return root;
    if (key < root->data) root->left = deleteNode(root->left, key);
    else if (key > root->data) root->right = deleteNode(root->right, key);
    else {
        if (root->left == NULL) {
            struct Node* temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            struct Node* temp = root->left;
            free(root);
            return temp;
        }
        struct Node* temp = minValueNode(root->right);
        root->data = temp->data;
        root->right = deleteNode(root->right, temp->data);
    }
    return root;
}

void inorder(struct Node* root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d -> ", root->data);
        inorder(root->right);
    }
}

int main() {
    struct Node* root = NULL;
    root = insert(root, 30);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 10);
    root = insert(root, 25);
    root = insert(root, 50);

    root = deleteNode(root, 30);

    inorder(root);
    printf("null\n");
    return 0;
}
A10 -> 20 -> 25 -> 40 -> 50 -> null
B10 -> 20 -> 25 -> 30 -> 40 -> 50 -> null
C20 -> 25 -> 40 -> 50 -> null
D10 -> 25 -> 40 -> 50 -> null
Attempts:
2 left
💡 Hint
When deleting a node with two children, replace it with the smallest node in the right subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in BST delete function
What error will occur when running this delete function on a BST?
DSA C
struct Node* deleteNode(struct Node* root, int key) {
    if (root == NULL) return root;
    if (key < root->data) root->left = deleteNode(root->left, key);
    else if (key > root->data) root->right = deleteNode(root->right, key);
    else {
        if (root->left == NULL) {
            struct Node* temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            struct Node* temp = root->left;
            free(root);
            return temp;
        }
        struct Node* temp = minValueNode(root->left); // Error here
        root->data = temp->data;
        root->left = deleteNode(root->left, temp->data);
    }
    return root;
}
AThe function causes a segmentation fault due to null pointer dereference
BThe function deletes the wrong node because it uses the maximum from left subtree instead of minimum from right subtree
CThe function causes infinite recursion because it never deletes the node
DThe function works correctly with no errors
Attempts:
2 left
💡 Hint
Check which node should replace a node with two children in BST deletion.
🧠 Conceptual
expert
2:00remaining
Number of nodes after multiple deletions in BST
Given a BST with nodes inserted in this order: 50, 30, 70, 20, 40, 60, 80. After deleting nodes 20, 30, and 50, how many nodes remain in the BST?
A4
B6
C3
D5
Attempts:
2 left
💡 Hint
Count initial nodes, then subtract deleted nodes, considering replacements.

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 to maintain balance.
C. The node is replaced by its left child.
D. The node is simply removed and its parent's pointer is set to null.

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: Update parent's pointer

    After removal, the parent's pointer to this node is set to null to disconnect it.
  3. Final Answer:

    The node is simply removed and its parent's pointer is set to null. -> Option D
  4. Quick Check:

    Leaf node deletion = remove node and set parent's pointer null [OK]
Hint: Leaf node deletion just removes node, no child replacement needed [OK]
Common Mistakes:
  • Replacing leaf node with child when none exists
  • Restructuring entire tree unnecessarily
  • Confusing leaf node with internal node deletion
2. Which of the following is the correct syntax to delete a node with value key from a BST in C?
easy
A. root = deleteNode(root, key);
B. deleteNode(root, key);
C. delete(root, key);
D. root.deleteNode(key);

Solution

  1. Step 1: Understand function call and assignment

    Deleting a node may change the root, so the returned root must be assigned back.
  2. Step 2: Check syntax correctness

    root = deleteNode(root, key); correctly assigns the returned root from deleteNode to root.
  3. Final Answer:

    root = deleteNode(root, key); -> Option A
  4. Quick Check:

    Assign returned root after deletion [OK]
Hint: Always assign returned root after deletion to handle root changes [OK]
Common Mistakes:
  • Not assigning returned root causing lost tree updates
  • Using incorrect function names
  • Using object-oriented syntax in C
3. Given the BST below and the code snippet to delete node with value 15:
      20
     /  \
   10    30
     \     \
     15    40

root = deleteNode(root, 15);
What will be the in-order traversal of the BST after deletion?
medium
A. [20, 30, 40]
B. [10, 15, 20, 30, 40]
C. [10, 20, 30, 40]
D. [10, 20, 15, 30, 40]

Solution

  1. Step 1: Delete node 15 from BST

    Node 15 is a leaf node, so it is removed directly.
  2. Step 2: Perform in-order traversal after deletion

    In-order traversal visits nodes in ascending order: 10, 20, 30, 40.
  3. Final Answer:

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

    In-order after deleting 15 = [10, 20, 30, 40] [OK]
Hint: In-order traversal after leaf deletion excludes that node [OK]
Common Mistakes:
  • Including deleted node in traversal
  • Confusing traversal order
  • Assuming node replaced incorrectly
4. Consider this buggy C code snippet for deleting a node from BST:
struct Node* deleteNode(struct Node* root, int key) {
  if (root == NULL) return root;
  if (key < root->data)
    deleteNode(root->left, key);
  else if (key > root->data)
    deleteNode(root->right, key);
  else {
    // deletion logic here
  }
  return root;
}
What is the main bug in this code?
medium
A. The deletion logic is missing for leaf nodes.
B. The recursive calls do not update root->left or root->right pointers.
C. The function does not handle the case when root is NULL.
D. The function returns root instead of void.

Solution

  1. Step 1: Analyze recursive calls

    Recursive calls are made but their results are not assigned back to root->left or root->right.
  2. Step 2: Understand pointer update necessity

    Without updating pointers, the tree structure won't change after deletion.
  3. Final Answer:

    The recursive calls do not update root->left or root->right pointers. -> Option B
  4. Quick Check:

    Update child pointers with recursive call results [OK]
Hint: Always assign recursive call results back to child pointers [OK]
Common Mistakes:
  • Ignoring pointer updates after recursion
  • Missing base case for NULL root
  • Confusing return types
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. The minimum node from the right subtree (60)
B. The maximum node from the left subtree (40)
C. The node 70 directly replaces 50
D. The node 30 directly replaces 50

Solution

  1. Step 1: Identify node to replace root on deletion

    When deleting a node with two children, replace it with the inorder successor (minimum in right subtree).
  2. Step 2: Find minimum node in right subtree of 50

    Right subtree nodes: 70, 60, 80. Minimum is 60.
  3. Final Answer:

    The minimum node from the right subtree (60) -> Option A
  4. Quick Check:

    Root replacement = inorder successor (min right subtree) [OK]
Hint: Replace deleted node with inorder successor (min right subtree) [OK]
Common Mistakes:
  • Replacing with max left subtree instead of min right
  • Replacing with direct child without checking BST rules
  • Confusing inorder predecessor and successor
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