0
0
DSA C++programming~20 mins

Maximum Path Sum in Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Maximum Path Sum Calculation
What is the output of the following C++ code that calculates the maximum path sum in a binary tree?
DSA C++
struct TreeNode {
    int val;
    TreeNode *left, *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

int maxPathSumUtil(TreeNode* root, int& maxSum) {
    if (!root) return 0;
    int left = std::max(0, maxPathSumUtil(root->left, maxSum));
    int right = std::max(0, maxPathSumUtil(root->right, maxSum));
    maxSum = std::max(maxSum, left + right + root->val);
    return root->val + std::max(left, right);
}

int maxPathSum(TreeNode* root) {
    int maxSum = INT_MIN;
    maxPathSumUtil(root, maxSum);
    return maxSum;
}

int main() {
    TreeNode* root = new TreeNode(-10);
    root->left = new TreeNode(9);
    root->right = new TreeNode(20);
    root->right->left = new TreeNode(15);
    root->right->right = new TreeNode(7);
    std::cout << maxPathSum(root);
    return 0;
}
A20
B35
C27
D42
Attempts:
2 left
💡 Hint
Consider the path that includes nodes 15, 20, and 7.
🧠 Conceptual
intermediate
1:30remaining
Understanding Path Sum Components
In the maximum path sum problem, why do we use std::max(0, left) and std::max(0, right) when calculating the maximum gain from left and right subtrees?
ATo ignore negative path sums that would reduce the total sum
BTo ensure the path always includes both left and right children
CTo count zero as a valid node value in the path
DTo handle the case when the tree is empty
Attempts:
2 left
💡 Hint
Think about how negative values affect the sum.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Maximum Path Sum Code
What error will the following code produce when run, and why?
DSA C++
int maxPathSumUtil(TreeNode* root, int& maxSum) {
    if (root == nullptr) return 0;
    int left = maxPathSumUtil(root->left, maxSum);
    int right = maxPathSumUtil(root->right, maxSum);
    maxSum = std::max(maxSum, left + right + root->val);
    return root->val + std::max(left, right);
}

int maxPathSum(TreeNode* root) {
    int maxSum = 0;
    maxPathSumUtil(root, maxSum);
    return maxSum;
}
AReturns incorrect maximum path sum when all node values are negative
BCauses a segmentation fault due to null pointer dereference
CCompiles but returns zero for any input tree
DThrows a runtime error due to uninitialized variable
Attempts:
2 left
💡 Hint
Check the initial value of maxSum and how negative values are handled.
🚀 Application
advanced
1:30remaining
Maximum Path Sum in a Tree with Negative and Positive Values
Given the binary tree below, what is the maximum path sum? 2 / \ -1 3 / \ -2 4
A6
B7
C9
D5
Attempts:
2 left
💡 Hint
Consider the path that includes nodes 2, 3, and 4.
Predict Output
expert
2:00remaining
Output of Maximum Path Sum with Single Node Negative Tree
What is the output of the following code when the tree has a single node with value -5?
DSA C++
struct TreeNode {
    int val;
    TreeNode *left, *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

int maxPathSumUtil(TreeNode* root, int& maxSum) {
    if (!root) return 0;
    int left = std::max(0, maxPathSumUtil(root->left, maxSum));
    int right = std::max(0, maxPathSumUtil(root->right, maxSum));
    maxSum = std::max(maxSum, left + right + root->val);
    return root->val + std::max(left, right);
}

int maxPathSum(TreeNode* root) {
    int maxSum = INT_MIN;
    maxPathSumUtil(root, maxSum);
    return maxSum;
}

int main() {
    TreeNode* root = new TreeNode(-5);
    std::cout << maxPathSum(root);
    return 0;
}
A0
B-5
C5
DINT_MIN
Attempts:
2 left
💡 Hint
The maximum path sum can be a single node value if all others are negative or absent.