0
0
DSA Typescriptprogramming~10 mins

Maximum Path Sum in Binary Tree in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Maximum Path Sum in Binary Tree
Start at root node
Calculate max path sum from left subtree
Calculate max path sum from right subtree
Calculate max path through current node
Update global max if current path sum is higher
Return max path sum including current node and one subtree
Repeat for all nodes
Final global max is the answer
Start from root, recursively find max path sums from left and right children, update global max with path through current node, return max path including one child to parent.
Execution Sample
DSA Typescript
function maxPathSum(root: TreeNode): number {
  let maxSum = -Infinity;
  function dfs(node: TreeNode | null): number {
    if (!node) return 0;
    const left = Math.max(dfs(node.left), 0);
    const right = Math.max(dfs(node.right), 0);
    maxSum = Math.max(maxSum, node.val + left + right);
    return node.val + Math.max(left, right);
  }
  dfs(root);
  return maxSum;
}
Recursively computes max path sum in binary tree, updating global max sum at each node.
Execution Table
StepOperationNode VisitedLeft MaxRight MaxCurrent Path SumGlobal MaxReturn ValueVisual State
1Visit node1001 + 0 + 0 = 111 + max(0,0) = 11
2Visit node2002 + 0 + 0 = 222 + max(0,0) = 21 -> 2
3Back to node1201 + 2 + 0 = 331 + max(2,0) = 31 -> 2
4Visit node300-3 + 0 + 0 = -33-3 + max(0,0) = 01 -> 2, 3
5Back to node1201 + 2 + 0 = 3331 -> 2, 3
6Finalroot---3-Final max path sum = 3
💡 All nodes visited, global max updated to 3
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
maxSum-Infinity123333
left-00202-
right-00000-
return value-12303-
Key Moments - 3 Insights
Why do we use Math.max(dfs(node.left), 0) instead of just dfs(node.left)?
Because negative path sums reduce the total, we ignore them by taking max with 0. See execution_table rows 2 and 4 where left or right max is 0 instead of negative.
Why do we update global max with node.val + left + right instead of just one side?
Because the max path can pass through the current node connecting left and right subtrees. This is shown in execution_table row 3 where current path sum includes both left and right.
Why does the dfs function return node.val + max(left, right) and not include both sides?
Because when returning to parent, path must be linear (one branch). Including both sides would create a fork, which is not allowed in path definition. See return values in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what is the global max value?
A1
B3
C4
D2
💡 Hint
Check the 'Global Max' column at Step 3 in execution_table
At which step does the algorithm ignore a negative path sum by using 0 instead?
AStep 4
BStep 2
CStep 1
DStep 5
💡 Hint
Look at 'Left Max' and 'Right Max' columns for negative values replaced by 0 in execution_table
If the node value at step 4 was positive instead of -3, how would the global max change?
AIt would stay the same
BIt would decrease
CIt would increase
DIt would become zero
💡 Hint
Consider how global max updates with current path sum in execution_table rows
Concept Snapshot
Maximum Path Sum in Binary Tree:
- Use DFS to explore each node
- Calculate max path sum from left and right children, ignore negatives
- Update global max with node.val + left + right
- Return node.val + max(left, right) to parent
- Final global max is the answer
Full Transcript
This visualization shows how to find the maximum path sum in a binary tree. We start at the root and recursively calculate the maximum path sums from the left and right children. Negative sums are ignored by taking the maximum with zero. At each node, we calculate the path sum that passes through it by adding its value and the maximum sums from both children. We update a global maximum if this sum is higher than before. When returning to the parent, we only include the node's value plus the larger of the two child sums to keep the path linear. The final global maximum after visiting all nodes is the maximum path sum in the tree.