Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only one node for nil instead of both.
Using wrong variable names.
✗ Incorrect
We check if the first node 'p' is nil and the second node 'q' is nil to confirm symmetry at this point.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong variable for nil.
Using && instead of || in the condition.
✗ Incorrect
We check if either 'p' or 'q' is nil. If one is nil and the other is not, trees are not symmetric.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with wrong variable.
Using assignment '=' instead of comparison '!='.
✗ Incorrect
We compare the value of node 'p' with node 'q' to check if they are equal for symmetry.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase field names which are incorrect in Go.
Swapping the subtree checks incorrectly.
✗ Incorrect
We check left subtree of p with right subtree of q and right subtree of p with left subtree of q to confirm symmetry.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing root.Left or root.Right instead of root.
Adding extra fields that cause errors.
✗ Incorrect
The helper function is called with the root node twice without accessing subfields here.