Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the BST node is nil.
DSA Go
if root [1] nil { return false }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes the logic to be reversed.
Using comparison operators like '<' or '>' with nil causes errors.
✗ Incorrect
We check if the root is equal to nil to know if we reached the end of a branch.
2fill in blank
mediumComplete the code to insert a value into the map to track visited numbers.
DSA Go
visited[[1]] = true Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'target' or 'sum' instead of the node's value.
Using 'complement' which is the needed pair value, not the current node.
✗ Incorrect
We mark the current node's value as visited by setting visited[root.Val] = true.
3fill in blank
hardFix the error in the condition to check if the complement exists in the map.
DSA Go
if _, ok := visited[[1]]; ok { return true }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction.
Reversing the subtraction order.
✗ Incorrect
The complement is target minus the current node's value to find the needed pair.
4fill in blank
hardFill both blanks to recursively search left and right subtrees.
DSA Go
return dfs(root.[1], target, visited) || dfs(root.[2], target, visited)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'Left' or 'Right' causes compilation errors.
Swapping left and right fields.
✗ Incorrect
In Go, struct fields start with lowercase letters for unexported fields, so use 'left' and 'right'.
5fill in blank
hardFill all three blanks to complete the TwoSumBST function using DFS and a map.
DSA Go
func TwoSumBST(root *TreeNode, target int) bool {
visited := make(map[int]bool)
var dfs func(node *TreeNode, target int, visited map[int]bool) bool
dfs = func(node *TreeNode, target int, visited map[int]bool) bool {
if node [1] nil {
return false
}
complement := target [2] node.Val
if _, ok := visited[complement]; ok {
return true
}
visited[node.Val] = true
return dfs(node.[3], target, visited) || dfs(node.right, target, visited)
}
return dfs(root, target, visited)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for nil check.
Adding instead of subtracting for complement.
Using uppercase 'Left' instead of 'left'.
✗ Incorrect
Check if node is nil with '==', find complement by subtracting node.Val from target, and recurse left subtree with 'left'.