Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

BST Delete Operation in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Delete Mastery
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 2?
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

class BST {
  root: Node | null = null;

  insert(val: number) {
    this.root = this._insert(this.root, val);
  }

  private _insert(root: Node | null, val: number): Node {
    if (!root) return new Node(val);
    if (val < root.val) root.left = this._insert(root.left, val);
    else root.right = this._insert(root.right, val);
    return root;
  }

  delete(val: number) {
    this.root = this._delete(this.root, val);
  }

  private _delete(root: Node | null, val: number): Node | null {
    if (!root) return null;
    if (val < root.val) root.left = this._delete(root.left, val);
    else if (val > root.val) root.right = this._delete(root.right, val);
    else {
      if (!root.left && !root.right) return null;
      if (!root.left) return root.right;
      if (!root.right) return root.left;
      let minLargerNode = this._minValueNode(root.right);
      root.val = minLargerNode.val;
      root.right = this._delete(root.right, minLargerNode.val);
    }
    return root;
  }

  private _minValueNode(root: Node): Node {
    while (root.left) root = root.left;
    return root;
  }

  printInOrder() {
    const result: number[] = [];
    function inorder(node: Node | null) {
      if (!node) return;
      inorder(node.left);
      result.push(node.val);
      inorder(node.right);
    }
    inorder(this.root);
    console.log(result.join(' -> ') + ' -> null');
  }
}

const bst = new BST();
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(2);
bst.insert(4);
bst.delete(2);
bst.printInOrder();
A3 -> 4 -> 5 -> 7 -> null
B2 -> 3 -> 4 -> 5 -> 7 -> null
C3 -> 5 -> 7 -> null
D4 -> 5 -> 7 -> null
Attempts:
2 left
💡 Hint
Deleting a leaf node removes it without affecting 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 3 which has one child?
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

class BST {
  root: Node | null = null;

  insert(val: number) {
    this.root = this._insert(this.root, val);
  }

  private _insert(root: Node | null, val: number): Node {
    if (!root) return new Node(val);
    if (val < root.val) root.left = this._insert(root.left, val);
    else root.right = this._insert(root.right, val);
    return root;
  }

  delete(val: number) {
    this.root = this._delete(this.root, val);
  }

  private _delete(root: Node | null, val: number): Node | null {
    if (!root) return null;
    if (val < root.val) root.left = this._delete(root.left, val);
    else if (val > root.val) root.right = this._delete(root.right, val);
    else {
      if (!root.left && !root.right) return null;
      if (!root.left) return root.right;
      if (!root.right) return root.left;
      let minLargerNode = this._minValueNode(root.right);
      root.val = minLargerNode.val;
      root.right = this._delete(root.right, minLargerNode.val);
    }
    return root;
  }

  private _minValueNode(root: Node): Node {
    while (root.left) root = root.left;
    return root;
  }

  printInOrder() {
    const result: number[] = [];
    function inorder(node: Node | null) {
      if (!node) return;
      inorder(node.left);
      result.push(node.val);
      inorder(node.right);
    }
    inorder(this.root);
    console.log(result.join(' -> ') + ' -> null');
  }
}

const bst = new BST();
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(4);
bst.delete(3);
bst.printInOrder();
A3 -> 4 -> 5 -> 7 -> null
B4 -> 5 -> 7 -> null
C5 -> 7 -> null
D4 -> 7 -> null
Attempts:
2 left
💡 Hint
When deleting a node with one child, the child replaces the deleted node.
❓ Predict Output
advanced
2:30remaining
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 Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

class BST {
  root: Node | null = null;

  insert(val: number) {
    this.root = this._insert(this.root, val);
  }

  private _insert(root: Node | null, val: number): Node {
    if (!root) return new Node(val);
    if (val < root.val) root.left = this._insert(root.left, val);
    else root.right = this._insert(root.right, val);
    return root;
  }

  delete(val: number) {
    this.root = this._delete(this.root, val);
  }

  private _delete(root: Node | null, val: number): Node | null {
    if (!root) return null;
    if (val < root.val) root.left = this._delete(root.left, val);
    else if (val > root.val) root.right = this._delete(root.right, val);
    else {
      if (!root.left && !root.right) return null;
      if (!root.left) return root.right;
      if (!root.right) return root.left;
      let minLargerNode = this._minValueNode(root.right);
      root.val = minLargerNode.val;
      root.right = this._delete(root.right, minLargerNode.val);
    }
    return root;
  }

  private _minValueNode(root: Node): Node {
    while (root.left) root = root.left;
    return root;
  }

  printInOrder() {
    const result: number[] = [];
    function inorder(node: Node | null) {
      if (!node) return;
      inorder(node.left);
      result.push(node.val);
      inorder(node.right);
    }
    inorder(this.root);
    console.log(result.join(' -> ') + ' -> null');
  }
}

const bst = new BST();
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(2);
bst.insert(4);
bst.insert(6);
bst.insert(8);
bst.delete(5);
bst.printInOrder();
A2 -> 3 -> 4 -> 6 -> 7 -> 8 -> null
B2 -> 3 -> 4 -> 7 -> 8 -> null
C3 -> 4 -> 6 -> 7 -> 8 -> null
D2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 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 method
What error will this code raise when trying to delete a node in the BST?
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

class BST {
  root: Node | null = null;

  delete(val: number) {
    this.root = this._delete(this.root, val);
  }

  private _delete(root: Node | null, val: number): Node | null {
    if (!root) return null;
    if (val < root.val) root.left = this._delete(root.left, val);
    else if (val > root.val) root.right = this._delete(root.right, val);
    else {
      if (!root.left && !root.right) return null;
      if (!root.left) return root.right;
      if (!root.right) return root.left;
      let minLargerNode = this._minValueNode(root.right);
      root.val = minLargerNode.val;
      root.right = this._delete(root.right, minLargerNode.val);
    }
    return root;
  }

  private _minValueNode(root: Node): Node {
    while (root.left) root = root.left;
    return root;
  }
}

const bst = new BST();
bst.delete(10);
ASyntaxError: Unexpected token
BNo error, deletion works fine
CReferenceError: _delete is not defined
DTypeError: Cannot read property 'val' of null
Attempts:
2 left
💡 Hint
Deleting from an empty tree tries to access properties of null.
🧠 Conceptual
expert
1:30remaining
Number of nodes after multiple deletions in BST
Given a BST with nodes inserted in this order: 10, 5, 15, 3, 7, 12, 18. After deleting nodes 5 and 15, how many nodes remain in the BST?
A6
B7
C5
D4
Attempts:
2 left
💡 Hint
Each deletion removes exactly one node from the tree.

Practice

(1/5)
1. What happens when you delete a leaf node (a node with no children) in a Binary Search Tree (BST)?
easy
A. The entire tree is rebuilt
B. The node is replaced by its right child
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 deleting it means just removing it from the tree.
  2. Step 2: Effect on the BST

    Since it has no children, no other nodes need to be moved or replaced.
  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 children need rearrangement
  • Replacing leaf with child nodes
  • Rebuilding the whole tree
2. Which of the following is the correct TypeScript function signature for deleting a node from a BST?
easy
A. function deleteNode(key: number): TreeNode
B. function deleteNode(root: number, key: TreeNode): void
C. function deleteNode(root: TreeNode | null, key: number): TreeNode | null
D. function deleteNode(root: TreeNode, key: string): TreeNode

Solution

  1. Step 1: Analyze parameter types

    The root should be a TreeNode or null, and key should be a number to identify the node.
  2. Step 2: Analyze return type

    The function returns the updated root node or null if tree becomes empty.
  3. Final Answer:

    function deleteNode(root: TreeNode | null, key: number): TreeNode | null -> Option C
  4. Quick Check:

    Correct parameter and return types = function deleteNode(root: TreeNode | null, key: number): TreeNode | null [OK]
Hint: Root can be null; key is number; returns updated root [OK]
Common Mistakes:
  • Swapping parameter types
  • Using wrong return type
  • Using string instead of number for key
3. Given the BST below and the delete operation deleteNode(root, 10), what is the in-order traversal of the BST after deletion?

BST structure:
15
/ \
10 20
/ \ \
8 12 25
medium
A. [8, 12, 15, 20, 25]
B. [8, 10, 12, 15, 20, 25]
C. [8, 15, 20, 25]
D. [12, 15, 20, 25]

Solution

  1. Step 1: Delete node 10 with two children

    Node 10 has two children (8 and 12). Replace it with its inorder successor, which is 12.
  2. Step 2: Remove node 12 from its original position

    After replacement, remove node 12 from right subtree of 10.
  3. Step 3: In-order traversal after deletion

    Traversal is [8, 12, 15, 20, 25].
  4. Final Answer:

    [8, 12, 15, 20, 25] -> Option A
  5. Quick Check:

    Inorder after deleting 10 = [8, 12, 15, 20, 25] [OK]
Hint: Replace node with inorder successor for two children [OK]
Common Mistakes:
  • Not replacing node 10 correctly
  • Leaving node 10 in tree
  • Incorrect traversal order
4. Consider this TypeScript code snippet for deleting a node from a BST. What is the bug?
function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
  if (!root) return null;
  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) return root.right;
    if (!root.right) return root.left;
    let minNode = findMin(root.right);
    root.val = minNode.val;
    deleteNode(root.right, minNode.val);
  }
  return root;
}
medium
A. No return statement after deletion
B. Missing assignment of root.right after deleting minNode
C. Wrong comparison operators for key
D. Incorrect base case for empty root

Solution

  1. Step 1: Analyze deletion of minNode

    After copying minNode's value, the code calls deleteNode on root.right but does not assign the result back to root.right.
  2. Step 2: Effect of missing assignment

    Without assignment, the minNode is not removed from the right subtree, causing duplicate values.
  3. Final Answer:

    Missing assignment of root.right after deleting minNode -> Option B
  4. Quick Check:

    Assign updated subtree after deletion [OK]
Hint: Always assign recursive delete result back to subtree [OK]
Common Mistakes:
  • Forgetting to assign after recursive delete
  • Misplacing base case
  • Using wrong comparison signs
5. You have a BST with nodes: 30, 20, 40, 10, 25, 35, 50. You delete node 30 (root). Which node becomes the new root after deletion using the standard BST delete operation?
hard
A. 35
B. 25
C. 40
D. 20

Solution

  1. Step 1: Identify node 30's children

    Node 30 has two children: left subtree with 20 and right subtree with 40.
  2. Step 2: Find inorder successor of 30

    Inorder successor is the smallest node in right subtree, which is 35.
  3. Step 3: Replace 30 with 35 and delete 35 from right subtree

    35 replaces 30 as root, and 35 is removed from its original position.
  4. Final Answer:

    35 -> Option A
  5. Quick Check:

    Inorder successor replaces deleted node [OK]
Hint: Use inorder successor (smallest right subtree) for two-child node deletion [OK]
Common Mistakes:
  • Using inorder predecessor instead
  • Choosing wrong replacement node
  • Not updating root after 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