0
0
DSA Cprogramming~20 mins

DP on Trees Maximum Path Sum in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DP on Trees 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 in a Simple Binary Tree
What is the output of the following code that calculates the maximum path sum in a binary tree?
DSA C
struct Node {
    int val;
    struct Node* left;
    struct Node* right;
};

int max(int a, int b) { return (a > b) ? a : b; }

int maxPathSumUtil(struct Node* root, int* res) {
    if (!root) return 0;
    int left = maxPathSumUtil(root->left, res);
    int right = maxPathSumUtil(root->right, res);
    int max_single = max(max(left, right) + root->val, root->val);
    int max_top = max(max_single, left + right + root->val);
    if (*res < max_top) *res = max_top;
    return max_single;
}

int maxPathSum(struct Node* root) {
    int res = -1000000;
    maxPathSumUtil(root, &res);
    return res;
}

// Tree structure:
//       1
//      / \
//     2   3

// Expected call: maxPathSum(root);
A6
B5
C3
D1
Attempts:
2 left
💡 Hint
Consider the path that includes both left and right children plus the root.
Predict Output
intermediate
2:00remaining
Maximum Path Sum with Negative Values
What is the output of the maximum path sum function for the following tree with negative values?
DSA C
// Tree structure:
//       -10
//       /  \
//      9   20
//          / \
//         15  7

// Using the same maxPathSum function as before.
A42
B35
C27
D-10
Attempts:
2 left
💡 Hint
The maximum path may not include the root if it reduces the sum.
🧠 Conceptual
advanced
1:30remaining
Understanding the Role of max_single and max_top in DP on Trees
In the DP solution for maximum path sum on trees, what does the variable max_single represent?
AThe maximum path sum excluding the current node
BThe maximum path sum including the current node and at most one child path
CThe maximum path sum including the current node and both children paths
DThe sum of values of all nodes in the tree
Attempts:
2 left
💡 Hint
Think about the path that can be extended upwards to the parent.
Predict Output
advanced
1:00remaining
Output of Maximum Path Sum in a Tree with Single Node
What is the output of the maximum path sum function when the tree has only one node with value -5?
DSA C
// Tree structure:
//    -5

// Using the same maxPathSum function as before.
AINT_MIN
B0
C5
D-5
Attempts:
2 left
💡 Hint
The maximum path sum in a single node tree is the node's value itself.
🧠 Conceptual
expert
2:00remaining
Why is it necessary to track a global maximum in DP on Trees Maximum Path Sum?
Why does the DP solution for maximum path sum on trees use a global variable to track the maximum path sum instead of returning it directly from the recursive function?
ABecause the global variable stores the sum of all leaf nodes only
BBecause the recursive function cannot return integers in C
CBecause the maximum path can be anywhere in the tree and may not pass through the root of the current subtree
DBecause the maximum path sum is always the sum of all nodes in the tree
Attempts:
2 left
💡 Hint
Consider paths that do not include the parent node.