Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is NULL.
DSA C
if (root [1] NULL) { return 0; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong logic.
Using '<=' or '>' is invalid for pointer comparison.
✗ Incorrect
We check if the root pointer is equal to NULL to know if the node exists or not.
2fill in blank
mediumComplete the code to check if the current node is a leaf node (no children).
DSA C
if (root->left [1] NULL && root->right == NULL) { return (root->val == targetSum); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong leaf detection.
Using '>' or '<' is invalid for pointer comparison.
✗ Incorrect
A leaf node has both left and right children as NULL, so we check if left child is NULL.
3fill in blank
hardFix the error in the recursive call to check left subtree with updated target sum.
DSA C
return hasPathSum(root->left, targetSum [1] root->val) || hasPathSum(root->right, targetSum - root->val);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' increases target sum incorrectly.
Using '*' or '/' is not meaningful here.
✗ Incorrect
We subtract the current node's value from targetSum when going down the tree.
4fill in blank
hardFill both blanks to complete the recursive function call for left and right subtrees.
DSA C
return hasPathSum(root->[1], targetSum - root->val) || hasPathSum(root->[2], targetSum - root->val);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'parent' instead of child pointers causes errors.
Swapping left and right does not affect correctness but is confusing.
✗ Incorrect
We recursively check left and right children for the path sum.
5fill in blank
hardFill all three blanks to complete the function signature and base case return value.
DSA C
int hasPathSum(struct TreeNode* [1], int [2]) { if ([3] == NULL) return 0;
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names causes confusion.
Checking wrong variable for NULL causes errors.
✗ Incorrect
The function takes root node and targetSum as parameters; base case checks if root is NULL.
