Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the term Maximum Path Sum in a binary tree mean?
easy
A. The sum of all node values in the tree
B. The largest sum of values along any path between two nodes in the tree
C. The maximum value of a single node in the tree
D. The sum of values from root to the deepest leaf only

Solution

  1. Step 1: Understand the path definition

    A path in a binary tree can start and end at any nodes, not necessarily root or leaf.
  2. Step 2: Define maximum path sum

    It is the highest sum of node values along any such path in the tree.
  3. Final Answer:

    The largest sum of values along any path between two nodes in the tree -> Option B
  4. Quick Check:

    Maximum path sum means highest sum on any path [OK]
Hint: Maximum path sum is any path, not just root to leaf [OK]
Common Mistakes:
  • Confusing maximum path sum with sum of all nodes
  • Thinking path must start at root
  • Considering only leaf nodes for path ends
2. Which of the following C++ function signatures is correct for a helper function that returns the maximum path sum starting from a given node in a binary tree?
easy
A. int maxPathSumHelper(TreeNode* root, int& maxSum);
B. void maxPathSumHelper(TreeNode root, int maxSum);
C. int maxPathSumHelper(TreeNode root);
D. int maxPathSumHelper(TreeNode* root);

Solution

  1. Step 1: Understand parameter passing

    The function needs to update maxSum globally, so it must be passed by reference.
  2. Step 2: Check pointer usage

    TreeNode* is used to pass node pointers, not objects directly.
  3. Final Answer:

    int maxPathSumHelper(TreeNode* root, int& maxSum); -> Option A
  4. Quick Check:

    Use pointer and reference for maxSum [OK]
Hint: Pass maxSum by reference and node as pointer [OK]
Common Mistakes:
  • Passing TreeNode by value instead of pointer
  • Not passing maxSum by reference
  • Using void return type instead of int
3. Consider the following binary tree:

    1
   / \
  2   3
What is the maximum path sum returned by the standard algorithm?
medium
A. 3
B. 5
C. 4
D. 6

Solution

  1. Step 1: Calculate path sums including root

    Paths: 2->1->3 sum = 6, 2->1 sum = 3, 1->3 sum = 4.
  2. Step 2: Identify maximum sum

    The maximum path sum is 6 from path 2->1->3.
  3. Final Answer:

    6 -> Option D
  4. Quick Check:

    2+1+3 = 6 [OK]
Hint: Check all paths including both children and root [OK]
Common Mistakes:
  • Considering only root-to-leaf paths
  • Ignoring paths passing through root
  • Adding only one child value
4. Given this buggy code snippet for maximum path sum, what is the main error?

int maxPathSumHelper(TreeNode* root, int& maxSum) {
  if (!root) return 0;
  int left = maxPathSumHelper(root->left, maxSum);
  int right = maxPathSumHelper(root->right, maxSum);
  int current = root->val + left + right;
  maxSum = max(maxSum, current);
  return root->val + max(left, right);
}
What is the bug?
medium
A. It does not ignore negative path sums
B. It returns sum of both children instead of max child
C. It updates maxSum incorrectly
D. It does not handle null root

Solution

  1. Step 1: Analyze negative path sums handling

    The code adds left and right directly without ignoring negative sums, which can reduce maxSum.
  2. Step 2: Correct approach for negatives

    Should use max(0, left) and max(0, right) to ignore negative sums.
  3. Final Answer:

    It does not ignore negative path sums -> Option A
  4. Quick Check:

    Negative sums must be ignored to maximize path sum [OK]
Hint: Use max(0, child) to ignore negative sums [OK]
Common Mistakes:
  • Adding negative child sums reduces maxSum
  • Returning sum of both children instead of max child
  • Not checking for null root
5. Given the binary tree below, what is the maximum path sum?

       -10
       /  \
      9   20
          / \
         15  7
Choose the correct maximum path sum.
hard
A. 27
B. 35
C. 42
D. 15

Solution

  1. Step 1: Calculate max path sums for subtrees

    Left subtree max path sum is 9. Right subtree max path sum includes 20 + 15 + 7 = 42.
  2. Step 2: Consider paths through root

    Path through root: -10 + max(0,9) + max(0,15+20+7) = -10 + 9 + 42 = 41, less than 42.
  3. Step 3: Identify maximum path sum

    The maximum path sum is 42 from path 15->20->7.
  4. Final Answer:

    42 -> Option C
  5. Quick Check:

    15+20+7 = 42 [OK]
Hint: Check all subtree sums and paths through root [OK]
Common Mistakes:
  • Including negative root value in max path sum
  • Ignoring paths not passing through root
  • Adding left and right children without max(0, val)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard