0
0
DSA Goprogramming~20 mins

BST Delete Operation in DSA Go - Practice Problems & Challenges

Choose your learning style9 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 Go
package main
import "fmt"
type Node struct {
    val   int
    left  *Node
    right *Node
}
func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{val: val}
    }
    if val < root.val {
        root.left = insert(root.left, val)
    } else {
        root.right = insert(root.right, val)
    }
    return root
}
func inorder(root *Node) {
    if root != nil {
        inorder(root.left)
        fmt.Print(root.val, " -> ")
        inorder(root.right)
    }
}
func minValueNode(node *Node) *Node {
    current := node
    for current.left != nil {
        current = current.left
    }
    return current
}
func deleteNode(root *Node, key int) *Node {
    if root == nil {
        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 == nil {
            return root.right
        } else if root.right == nil {
            return root.left
        }
        temp := minValueNode(root.right)
        root.val = temp.val
        root.right = deleteNode(root.right, temp.val)
    }
    return root
}
func main() {
    var root *Node
    vals := []int{50, 30, 20, 40, 70, 60, 80}
    for _, v := range vals {
        root = insert(root, v)
    }
    root = deleteNode(root, 20)
    inorder(root)
    fmt.Print("null\n")
}
A20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
B30 -> 40 -> 50 -> 60 -> 70 -> null
C30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
D50 -> 30 -> 40 -> 70 -> 60 -> 80 -> 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 30, which has one child?
DSA Go
package main
import "fmt"
type Node struct {
    val   int
    left  *Node
    right *Node
}
func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{val: val}
    }
    if val < root.val {
        root.left = insert(root.left, val)
    } else {
        root.right = insert(root.right, val)
    }
    return root
}
func inorder(root *Node) {
    if root != nil {
        inorder(root.left)
        fmt.Print(root.val, " -> ")
        inorder(root.right)
    }
}
func minValueNode(node *Node) *Node {
    current := node
    for current.left != nil {
        current = current.left
    }
    return current
}
func deleteNode(root *Node, key int) *Node {
    if root == nil {
        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 == nil {
            return root.right
        } else if root.right == nil {
            return root.left
        }
        temp := minValueNode(root.right)
        root.val = temp.val
        root.right = deleteNode(root.right, temp.val)
    }
    return root
}
func main() {
    var root *Node
    vals := []int{50, 30, 20, 40, 70, 60, 80}
    for _, v := range vals {
        root = insert(root, v)
    }
    root = deleteNode(root, 30)
    inorder(root)
    fmt.Print("null\n")
}
A20 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
B30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
C20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
D40 -> 50 -> 60 -> 70 -> 80 -> 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 50, which has two children?
DSA Go
package main
import "fmt"
type Node struct {
    val   int
    left  *Node
    right *Node
}
func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{val: val}
    }
    if val < root.val {
        root.left = insert(root.left, val)
    } else {
        root.right = insert(root.right, val)
    }
    return root
}
func inorder(root *Node) {
    if root != nil {
        inorder(root.left)
        fmt.Print(root.val, " -> ")
        inorder(root.right)
    }
}
func minValueNode(node *Node) *Node {
    current := node
    for current.left != nil {
        current = current.left
    }
    return current
}
func deleteNode(root *Node, key int) *Node {
    if root == nil {
        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 == nil {
            return root.right
        } else if root.right == nil {
            return root.left
        }
        temp := minValueNode(root.right)
        root.val = temp.val
        root.right = deleteNode(root.right, temp.val)
    }
    return root
}
func main() {
    var root *Node
    vals := []int{50, 30, 20, 40, 70, 60, 80}
    for _, v := range vals {
        root = insert(root, v)
    }
    root = deleteNode(root, 50)
    inorder(root)
    fmt.Print("null\n")
}
A30 -> 40 -> 60 -> 70 -> 80 -> null
B20 -> 30 -> 40 -> 60 -> 70 -> 80 -> null
C20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
D20 -> 30 -> 40 -> 60 -> 70 -> 80 -> 50 -> 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 function
What error will occur when running this BST delete function if the node to delete has no children?
DSA Go
func deleteNode(root *Node, key int) *Node {
    if root == nil {
        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 == nil {
            return root.right
        } else if root.right == nil {
            return root.left
        }
        temp := minValueNode(root.right)
        root.val = temp.val
        root.right = deleteNode(root.right, temp.val)
    }
    return root
}

// Assume minValueNode is correctly implemented.
ANo error, function works correctly for leaf nodes
BNil pointer dereference when deleting leaf node
CStack overflow due to infinite recursion
DCompilation error due to missing return statement
Attempts:
2 left
💡 Hint
Check how leaf nodes are handled in the code.
🧠 Conceptual
expert
2:00remaining
Minimum number of nodes in BST after deleting root with two children
Given a BST with 7 nodes inserted in this order: 50, 30, 20, 40, 70, 60, 80. After deleting the root node (50), what is the minimum number of nodes remaining in the BST?
A4
B5
C7
D6
Attempts:
2 left
💡 Hint
Deleting a node removes exactly one node from the tree.