Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is a leaf node.
DSA Go
if root.Left == nil && root.[1] == nil {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for Parent or Next instead of Right child.
Using incorrect field names.
✗ Incorrect
A leaf node has no left or right children, so both must be nil.
2fill in blank
mediumComplete the code to subtract the current node's value from the sum.
DSA Go
return hasPathSum(root.Left, sum - root.[1]) || hasPathSum(root.Right, sum - root.Val)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Value' or 'Data' which are not valid field names.
Using 'Num' which is not defined.
✗ Incorrect
The node's value is stored in the field 'Val' in Go binary tree nodes.
3fill in blank
hardFix the error in the base case to correctly return false when the node is nil.
DSA Go
if root == [1] { return false }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'null' or 'None' instead of 'nil'.
Using 0 which is not a pointer.
✗ Incorrect
In Go, the nil keyword is used to check if a pointer is empty or not pointing to any node.
4fill in blank
hardFill both blanks to correctly check if the current node is a leaf and if the sum matches the node's value.
DSA Go
if root.Left == nil && root.Right == nil { return sum == root.[1] } else { return hasPathSum(root.[2], sum - root.Val) || hasPathSum(root.Right, sum - root.Val) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Data' instead of 'Val'.
Using 'Right' for the first recursive call instead of 'Left'.
✗ Incorrect
The node's value is in 'Val'. For the recursive call, we check the left child first using 'Left'.
5fill in blank
hardFill all three blanks to complete the recursive calls checking both children with updated sums.
DSA Go
return hasPathSum(root.[1], sum - root.[2]) || hasPathSum(root.[3], sum - root.Val)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Data' instead of 'Val'.
Mixing up child pointers in recursive calls.
✗ Incorrect
We recurse on the left child with sum minus root.Val, and on the right child similarly.