0
0
DSA Goprogramming~10 mins

Maximum Path Sum in Binary Tree in DSA Go - 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 sum passing through current node
Update global max if current path sum is greater
Return max path sum for one side + current node to parent
Repeat for all nodes
Final global max is the answer
We visit each node, find max path sums from left and right children, update global max with paths passing through the node, and return max single-side path sum to parent.
Execution Sample
DSA Go
func maxPathSum(root *TreeNode) int {
  maxSum := math.MinInt32
  var dfs func(*TreeNode) int
  dfs = func(node *TreeNode) int {
    if node == nil { return 0 }
    left := max(dfs(node.Left), 0)
    right := max(dfs(node.Right), 0)
    maxSum = max(maxSum, node.Val + left + right)
    return node.Val + max(left, right)
  }
  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 Path SumGlobal MaxReturn ValueVisual State
1VisitNode 1 (10)???-∞?10
2VisitNode 2 (2)???-∞?2
3VisitNode 4 (20)0020-∞2020
4ReturnNode 40020202020
5VisitNode 5 (1)00120120 -> 1
6ReturnNode 500120120 -> 1
7CalculateNode 220123232210 -> 2 -> 20, 1
8ReturnNode 220123232210 -> 2 -> 20, 1
9VisitNode 3 (-25)???23?10 -> 2 -> 20, 1
10VisitNode 6 (3)00323310 -> 2 -> 20, 1
11ReturnNode 600323310 -> 2 -> 20, 1
12VisitNode 7 (4)00423410 -> 2 -> 20, 1
13ReturnNode 700423410 -> 2 -> 20, 1
14CalculateNode 334-1823-2110 -> 2 -> 20, 1
15ReturnNode 334-1823-2110 -> 2 -> 20, 1
16CalculateNode 122032323210 -> 2 -> 20, 1, -25 -> 3, 4
17ReturnNode 122032323210 -> 2 -> 20, 1, -25 -> 3, 4
18End----32-Final max path sum is 32
💡 All nodes visited, global max updated to 32, recursion ends.
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 7After Step 11After Step 13After Step 15After Step 17Final
maxSum-∞2020232323233232
Return Value at Node 4N/A2020202020202020
Return Value at Node 5N/AN/A1111111
Return Value at Node 2N/AN/AN/A222222222222
Return Value at Node 6N/AN/AN/AN/A33333
Return Value at Node 7N/AN/AN/AN/AN/A4444
Return Value at Node 3N/AN/AN/AN/AN/AN/A-21-21-21
Return Value at Node 1N/AN/AN/AN/AN/AN/AN/A3232
Key Moments - 3 Insights
Why do we take max(dfs(child), 0) instead of just dfs(child)?
Because negative path sums reduce the total, we ignore them by taking max with 0. See steps 3 and 5 where left and right max are 0 if 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 step 7 and step 16 where global max updates consider both sides.
What does the return value represent at each node?
It represents the max path sum for one side plus the current node, to be used by the parent. This is why in step 7 and 16, return values are node.Val + max(left, right).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the global max after visiting Node 2?
A23
B20
C22
D32
💡 Hint
Check the 'Global Max' column at step 7 in execution_table.
At which step does the recursion return the value 22 for Node 2?
AStep 4
BStep 7
CStep 6
DStep 17
💡 Hint
Look at the 'Return Value' column for Node 2 in execution_table.
If we did not ignore negative sums (did not use max with 0), what would happen to the global max at Node 3 (step 14)?
AIt would increase
BIt would stay the same
CIt would decrease
DIt would become zero
💡 Hint
Check how left and right max are 3 and 4, but node.Val is -25, so negative sums affect the total.
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 node.Val + left + right
- Return max single-side path sum 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 visit each node. For each node, we calculate the maximum path sums from its left and right children, ignoring negative sums by taking max with zero. We then calculate the path sum passing through the current node by adding its value and the left and right max sums. We update a global maximum if this path sum is greater. The function returns the maximum path sum for one side plus the current node to its parent. This process repeats for all nodes, and the final global maximum is the maximum path sum in the tree.