0
0
DSA Goprogramming~10 mins

Two Sum in BST in DSA Go - Interactive Practice

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

Complete 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'
A!=
B<
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes the logic to be reversed.
Using comparison operators like '<' or '>' with nil causes errors.
2fill in blank
medium

Complete 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'
Asum
Btarget
Ccomplement
Droot.Val
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.
3fill in blank
hard

Fix 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'
Atarget - root.Val
Broot.Val - target
Croot.Val + target
Dtarget + root.Val
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction.
Reversing the subtraction order.
4fill in blank
hard

Fill 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'
ALeft
BRight
Cleft
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'Left' or 'Right' causes compilation errors.
Swapping left and right fields.
5fill in blank
hard

Fill 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'
A==
B-
Cleft
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for nil check.
Adding instead of subtracting for complement.
Using uppercase 'Left' instead of 'left'.