Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong placement.
Using '==' or '!=' is incorrect for BST insertion.
✗ Incorrect
In a BST, values less than the current node go to the left subtree.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes search in wrong subtree.
Using '==' or '!=' in this condition is incorrect.
✗ Incorrect
When searching, if the value is less than current node, go left.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking right child leads to maximum, not minimum.
Accessing value or parent is not correct here.
✗ Incorrect
The minimum value in a BST is found by going as left as possible.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping '<' and '>' causes wrong traversal.
Using '==' or '!=' in these conditions is incorrect.
✗ Incorrect
To delete, traverse left if value is less, right if greater.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'depth' as key instead of node value.
Swapping left and right child names.
✗ Incorrect
Store node value as key, traverse left and right children recursively.