0
0
DSA C++programming~20 mins

Path Sum Root to Leaf in Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
šŸŽ–ļø
Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2:00remaining
Output of path sum check for given tree and sum
What is the output of the following C++ code that checks if there is a root-to-leaf path with sum 22?
DSA C++
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

bool hasPathSum(TreeNode* root, int sum) {
    if (!root) return false;
    if (!root->left && !root->right) return root->val == sum;
    return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}

int main() {
    TreeNode* root = new TreeNode(5);
    root->left = new TreeNode(4);
    root->right = new TreeNode(8);
    root->left->left = new TreeNode(11);
    root->left->left->left = new TreeNode(7);
    root->left->left->right = new TreeNode(2);
    root->right->left = new TreeNode(13);
    root->right->right = new TreeNode(4);
    root->right->right->right = new TreeNode(1);

    bool result = hasPathSum(root, 22);
    std::cout << (result ? "true" : "false") << std::endl;
    return 0;
}
ACompilation error
Bfalse
Ctrue
DRuntime error
Attempts:
2 left
šŸ’” Hint
Trace the path sums from root to leaf nodes carefully.
🧠 Conceptual
intermediate
2:00remaining
Number of root-to-leaf paths with given sum
Given a binary tree and a sum, how many root-to-leaf paths have the exact sum? Consider the tree below and sum = 26. Tree structure: 5 ā”œā”€4 │ └─11 │ ā”œā”€7 │ └─2 └─8 ā”œā”€13 └─4 └─1
A1
B2
C3
D0
Attempts:
2 left
šŸ’” Hint
Check all root-to-leaf paths and sum their values.
šŸ”§ Debug
advanced
2:00remaining
Identify the error in path sum function
What error does the following code produce when checking for path sum in a binary tree?
DSA C++
bool hasPathSum(TreeNode* root, int sum) {
    if (!root) return false;
    if (!root->left && !root->right) return root->val = sum;
    return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
ASyntax error due to assignment in return statement
BLogical error: always returns true
CRuntime error: null pointer dereference
DNo error, works correctly
Attempts:
2 left
šŸ’” Hint
Check the use of '=' vs '==' in the return statement.
šŸš€ Application
advanced
2:00remaining
Find all root-to-leaf paths with given sum
Which option correctly returns all root-to-leaf paths where the sum of node values equals the target sum?
DSA C++
void findPaths(TreeNode* root, int sum, std::vector<int>& path, std::vector<std::vector<int>>& result) {
    if (!root) return;
    path.push_back(root->val);
    if (!root->left && !root->right && root->val == sum) {
        result.push_back(path);
    } else {
        findPaths(root->left, sum - root->val, path, result);
        findPaths(root->right, sum - root->val, path, result);
    }
    path.pop_back();
}
AFails to backtrack path, causing incorrect results
BOnly finds one path even if multiple exist
CDoes not check leaf nodes properly
DCorrectly finds all paths with sum equal to target
Attempts:
2 left
šŸ’” Hint
Check if path is popped after recursion to backtrack.
ā“ Predict Output
expert
2:00remaining
Output of path sum check with negative values
What is the output of the following code snippet that checks for a path sum of 0 in a tree with negative values?
DSA C++
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

bool hasPathSum(TreeNode* root, int sum) {
    if (!root) return false;
    if (!root->left && !root->right) return root->val == sum;
    return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}

int main() {
    TreeNode* root = new TreeNode(-2);
    root->right = new TreeNode(-3);

    bool result = hasPathSum(root, 0);
    std::cout << (result ? "true" : "false") << std::endl;
    return 0;
}
Atrue
Bfalse
CCompilation error
DRuntime error
Attempts:
2 left
šŸ’” Hint
Check if any root-to-leaf path sums to zero.