0
0
DSA Javascriptprogramming~10 mins

Maximum Path Sum in Binary Tree in DSA Javascript - 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 including current node
Update global max if current path sum is higher
Return max path sum for one side + current node
Repeat for all nodes
Final max path sum stored globally
We visit each node, calculate max path sums from left and right children, update global max path sum including current node, and return max sum for one side to parent.
Execution Sample
DSA Javascript
function maxPathSum(root) {
  let maxSum = -Infinity;
  function dfs(node) {
    if (!node) return 0;
    let left = Math.max(dfs(node.left), 0);
    let right = Math.max(dfs(node.right), 0);
    maxSum = Math.max(maxSum, left + right + node.val);
    return Math.max(left, right) + node.val;
  }
  dfs(root);
  return maxSum;
}
This code finds the maximum path sum in a binary tree by recursively calculating max sums from children and updating a global max.
Execution Table
StepOperationNode VisitedLeft MaxRight MaxCurrent Max PathGlobal MaxReturn ValueVisual State
1Visit node10N/AN/AN/A-InfinityN/A10
2Visit node2N/AN/AN/A-InfinityN/A2
3Calculate left max20022210 -> 2
4Visit node102N/AN/A2N/A10
5Visit node10210N/A2N/A10 -> 2, 10
6Calculate max path1021022222010 -> 2, 10
7Visit node-25N/AN/AN/A22N/A10 -> 2, 10
8Visit node3N/AN/AN/A22N/A10 -> 2, 10
9Calculate left max300322310 -> 2, 10
10Visit node-253N/AN/A22N/A10 -> 2, 10
11Visit node4N/AN/AN/A22N/A10 -> 2, 10
12Calculate right max-2534-1822-1810 -> 2, 10
13Calculate max path-2534-1822-1810 -> 2, 10
14Return to parent-2534-1822-1810 -> 2, 10
15Calculate max path1021022222010 -> 2, 10
16Update global max1021022222010 -> 2, 10
17Return to root1021022222010 -> 2, 10
18Final max path sumrootN/AN/AN/A22N/A10 -> 2, 10
💡 All nodes visited, global max path sum is 22
Variable Tracker
VariableStartAfter Step 3After Step 9After Step 12After Step 16Final
maxSum-Infinity222222222
leftN/A00322
rightN/A0041010
returnValueN/A23-182020
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 3 and 9 where left or right max is 0 instead of negative.
Why do we update global max with left + right + node.val?
Because the max path can pass through the current node connecting left and right subtrees. This is shown in execution_table row 6 where global max is updated with sum including both sides.
What does the return value of dfs represent?
It represents the max path sum for one side plus current node to be used by parent. It never includes both sides to avoid double counting. See returnValue column in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the global max path sum?
A10
B12
C22
D-Infinity
💡 Hint
Check the Global Max column at step 6 in execution_table
At which step does the left max path sum for node with value 3 get calculated?
AStep 9
BStep 3
CStep 12
DStep 16
💡 Hint
Look at the Left Max column and Node Visited column in execution_table
If we remove Math.max(..., 0) and allow negative sums, how would the global max change?
AIt would stay the same
BIt could decrease because negative paths reduce sums
CIt would always increase
DIt would cause an error
💡 Hint
Refer to key_moments about ignoring negative sums and execution_table rows where 0 replaces negatives
Concept Snapshot
Maximum Path Sum in Binary Tree:
- Visit each node recursively
- Calculate max path sum from left and right children (ignore negatives)
- Update global max with left + right + node value
- Return max path sum for one side + node value
- Final global max is answer
Full Transcript
This visualization shows how to find the maximum path sum in a binary tree. We start at the root and recursively visit each node. For each node, we calculate the maximum path sum from its left and right children, ignoring negative sums by taking the maximum with zero. We then update a global maximum path sum if the sum of left max, right max, and the current node's value is greater than the current global max. The function returns the maximum path sum for one side plus the current node's value to its parent. This process repeats for all nodes, and the final global max is the maximum path sum in the tree.