0
0
DSA C++programming~10 mins

Path Sum Root to Leaf in Binary Tree in DSA C++ - 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 a leaf node.

DSA C++
if (root->left == nullptr && root->right == [1]) {
    return sum == root->val;
}
Drag options to blanks, or click blank then click option'
A0
BNULL
Cnullptr
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr.
Checking only one child pointer.
2fill in blank
medium

Complete the code to recursively check the left subtree for the path sum.

DSA C++
if (root->left != nullptr) {
    if (hasPathSum(root->left, sum - [1])) {
        return true;
    }
}
Drag options to blanks, or click blank then click option'
Aroot->val
Bsum
Croot->left->val
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting sum instead of root->val.
Using the child's value instead of current node's value.
3fill in blank
hard

Fix the error in the base case to handle null nodes correctly.

DSA C++
if (root == [1]) {
    return false;
}
Drag options to blanks, or click blank then click option'
Anullptr
BNULL
C0
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL or 0 instead of nullptr.
Checking root against root itself.
4fill in blank
hard

Fill both blanks to recursively check left and right subtrees for the path sum.

DSA C++
return hasPathSum(root->[1], sum - root->val) || hasPathSum(root->[2], sum - root->val);
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cval
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using val or parent instead of left or right.
Not subtracting root->val from sum.
5fill in blank
hard

Fill all three blanks to complete the hasPathSum function.

DSA C++
bool hasPathSum(TreeNode* root, int sum) {
    if (root == [1]) return false;
    if (root->left == nullptr && root->right == nullptr) {
        return sum == root->[2];
    }
    return hasPathSum(root->[3], sum - root->val) || hasPathSum(root->right, sum - root->val);
}
Drag options to blanks, or click blank then click option'
Anullptr
Bval
Cleft
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pointer names or values.
Not handling null root correctly.