0
0
DSA Goprogramming~10 mins

Why BST Over Plain Binary Tree in DSA Go - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a value into a Binary Search Tree (BST).

DSA Go
func insert(root *Node, val int) *Node {
    if root == nil {
        return &Node{value: val}
    }
    if val [1] root.value {
        root.left = insert(root.left, val)
    } else {
        root.right = insert(root.right, val)
    }
    return root
}
Drag options to blanks, or click blank then click option'
A!=
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong placement.
Using '==' or '!=' is incorrect for BST insertion.
2fill in blank
medium

Complete the code to search for a value in a BST.

DSA Go
func search(root *Node, val int) bool {
    if root == nil {
        return false
    }
    if val == root.value {
        return true
    } else if val [1] root.value {
        return search(root.left, val)
    } else {
        return search(root.right, val)
    }
}
Drag options to blanks, or click blank then click option'
A<
B==
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes search in wrong subtree.
Using '==' or '!=' in this condition is incorrect.
3fill in blank
hard

Fix the error in the code to find the minimum value node in a BST.

DSA Go
func findMin(root *Node) *Node {
    current := root
    for current.[1] != nil {
        current = current.left
    }
    return current
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cvalue
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Checking right child leads to maximum, not minimum.
Accessing value or parent is not correct here.
4fill in blank
hard

Fill both blanks to create a function that deletes a node from a BST.

DSA Go
func deleteNode(root *Node, val int) *Node {
    if root == nil {
        return root
    }
    if val [1] root.value {
        root.left = deleteNode(root.left, val)
    } else if val [2] root.value {
        root.right = deleteNode(root.right, val)
    } else {
        // Node with only one child or no child
        if root.left == nil {
            return root.right
        } else if root.right == nil {
            return root.left
        }
        // Node with two children
        temp := findMin(root.right)
        root.value = temp.value
        root.right = deleteNode(root.right, temp.value)
    }
    return root
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping '<' and '>' causes wrong traversal.
Using '==' or '!=' in these conditions is incorrect.
5fill in blank
hard

Fill all three blanks to create a function that returns a map of node values to their depths in a BST.

DSA Go
func mapDepths(root *Node, depth int, result map[int]int) map[int]int {
    if root == nil {
        return result
    }
    result[[1]] = depth
    mapDepths(root.[2], depth+1, result)
    mapDepths(root.[3], depth+1, result)
    return result
}
Drag options to blanks, or click blank then click option'
Aroot.value
Bleft
Cright
Ddepth
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'depth' as key instead of node value.
Swapping left and right child names.