0
0
DSA Goprogramming~10 mins

Check if Two Trees are Symmetric 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 both nodes are nil, which means symmetric at this point.

DSA Go
if [1] == nil && q == nil {
    return true
}
Drag options to blanks, or click blank then click option'
Ap
Bq
Croot
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only one node for nil instead of both.
Using wrong variable names.
2fill in blank
medium

Complete the code to check if either node is nil, which means trees are not symmetric.

DSA Go
if p == nil || [1] == nil {
    return false
}
Drag options to blanks, or click blank then click option'
Aq
Broot
Cnode
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong variable for nil.
Using && instead of || in the condition.
3fill in blank
hard

Fix the error in the comparison of node values to check if they are equal.

DSA Go
if p.Val != [1].Val {
    return false
}
Drag options to blanks, or click blank then click option'
Aroot
Bleft
Cnode
Dq
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with wrong variable.
Using assignment '=' instead of comparison '!='.
4fill in blank
hard

Fill both blanks to recursively check the left subtree of p with the right subtree of q, and the right subtree of p with the left subtree of q.

DSA Go
return isSymmetricHelper(p.[1], q.[2]) && isSymmetricHelper(p.[3], q.[4])
Drag options to blanks, or click blank then click option'
ALeft
BRight
Cleft
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase field names which are incorrect in Go.
Swapping the subtree checks incorrectly.
5fill in blank
hard

Fill both blanks to complete the main function that calls the helper with the root node twice.

DSA Go
func isSymmetric(root *TreeNode) bool {
    if root == nil {
        return true
    }
    return isSymmetricHelper(root[1], root[2])
}
Drag options to blanks, or click blank then click option'
A.Left
B.Right
D.Root
Attempts:
3 left
💡 Hint
Common Mistakes
Passing root.Left or root.Right instead of root.
Adding extra fields that cause errors.