0
0
DSA Goprogramming~10 mins

Lowest Common Ancestor in Binary Tree 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 current node is nil.

DSA Go
if root [1] nil {
    return nil
}
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 logic.
Using '<' or '>' with pointers is invalid.
2fill in blank
medium

Complete the code to check if the current node matches either target node p or q.

DSA Go
if root == [1] || root == [2] {
    return root
}
Drag options to blanks, or click blank then click option'
Ap
Bq
Cnil
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'nil' instead of target nodes.
Using 'root' instead of 'p' or 'q'.
3fill in blank
hard

Fix the error in the recursive calls to search left and right subtrees.

DSA Go
left := lowestCommonAncestor(root.[1], p, q)
right := lowestCommonAncestor(root.[2], p, q)
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.
Mixing field names with variable names.
4fill in blank
hard

Fill both blanks to return the correct node based on left and right subtree results.

DSA Go
if left != nil && right != nil {
    return [1]
}
if left != nil {
    return [2]
}
return right
Drag options to blanks, or click blank then click option'
Aroot
Bleft
Cright
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Returning left or right when both are non-nil.
Returning nil incorrectly.
5fill in blank
hard

Fill all three blanks to complete the full function for Lowest Common Ancestor.

DSA Go
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    if root [1] nil {
        return nil
    }
    if root == p || root == q {
        return root
    }
    left := lowestCommonAncestor(root.[2], p, q)
    right := lowestCommonAncestor(root.[3], p, q)
    if left != nil && right != nil {
        return root
    }
    if left != nil {
        return left
    }
    return right
}
Drag options to blanks, or click blank then click option'
A==
Bleft
Cright
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for nil check.
Using uppercase 'Left' or 'Right' fields.
Returning wrong nodes when both children are non-nil.