0
0
DSA Cprogramming~10 mins

DP on Trees Maximum Path Sum in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - DP on Trees Maximum Path Sum
Start at root node
Compute max path sum from left subtree
Compute max path sum from right subtree
Calculate max path through current node
Update global max if current path is better
Return max path sum including current node and one subtree
Repeat for all nodes
Final global max is answer
We start at the root and recursively compute max path sums from left and right children. At each node, we update the global max path sum considering paths through that node.
Execution Sample
DSA C
int maxPathSumUtil(Node* node, int* globalMax) {
  if (!node) return 0;
  int left = max(0, maxPathSumUtil(node->left, globalMax));
  int right = max(0, maxPathSumUtil(node->right, globalMax));
  *globalMax = max(*globalMax, left + right + node->val);
  return node->val + max(left, right);
}
This function computes max path sum including current node and updates global max path sum.
Execution Table
StepOperationNode VisitedLeft MaxRight MaxCurrent Max PathGlobal Max UpdatedReturn ValueVisual State
1VisitNode 1 (val=1)001111
2VisitNode 2 (val=2)002221 -> 2
3VisitNode 4 (val=4)004441 -> 2 -> 4
4ReturnNode 4004441 -> 2 -> 4
5ReturnNode 2406661 -> 2 -> 4
6VisitNode 3 (val=3)003631 -> 2 -> 4, 3
7ReturnNode 3003631 -> 2 -> 4, 3
8ReturnNode 163101071 -> 2 -> 4, 3
9End------Final max path sum = 10
💡 All nodes visited, global max path sum found
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5After Step 6After Step 8Final
globalMax-inf124661010
Return Value (Node 4)---4---4
Return Value (Node 2)----6--6
Return Value (Node 3)-----3-3
Return Value (Node 1)------77
Key Moments - 3 Insights
Why do we take max(0, left) and max(0, right) instead of just left and right?
Because negative path sums reduce the total, we ignore them by taking max with 0. See steps 3 and 6 where left or right max is 0 if subtree sum is negative.
Why do we update global max with left + right + node value?
Because the max path can pass through the current node connecting left and right subtrees. This is shown in step 8 where global max updates to 10.
Why does the function return node value plus max(left, right) instead of left + right + node value?
Because when returning to parent, path can only extend through one child, not both. The sum of both sides is only for updating global max, not for return value (see step 8 return value 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the global max after visiting Node 4?
A4
B2
C1
D6
💡 Hint
Check the 'Global Max Updated' column at step 4 in the execution table.
At which step does the global max path sum become 10?
AStep 6
BStep 8
CStep 5
DStep 3
💡 Hint
Look at the 'Global Max Updated' column and find when it reaches 10.
If Node 3 had a negative value, how would the return value at Node 1 change?
AIt would increase
BIt would stay the same
CIt would decrease
DIt would become zero
💡 Hint
Check the 'Return Value' column for Node 1 and consider how a negative right max affects it.
Concept Snapshot
DP on Trees Maximum Path Sum:
- Use recursion to get max path sums from left and right children
- Ignore negative sums by max(0, childSum)
- Update global max with left + right + node value
- Return node value + max(left, right) to parent
- Final global max is the answer
Full Transcript
This concept shows how to find the maximum path sum in a tree using dynamic programming. We start at the root and recursively compute the maximum path sums from left and right subtrees. At each node, we calculate the maximum path sum passing through it by adding the node's value and the maximum sums from left and right children, ignoring negative sums. We update a global maximum if this path is better. The function returns the maximum sum including the current node and one subtree to its parent. This process continues until all nodes are visited, and the global maximum holds the maximum path sum in the tree.