This problem follows the Post-order DFS pattern, commonly found in the Trees category. Recognizing this pattern is key to solving it efficiently in an interview setting.
At each node, compute max single-path sum. Update global max with left + node + right.
The 'split' vs 'continue' distinction: update the global max with the full path (left + node + right), but return only one side to the parent.
maxSum = -infinity
def dfs(node):
if not node: return 0
left = max(0, dfs(node.left))
right = max(0, dfs(node.right))
maxSum = max(maxSum, left + node.val + right)
return node.val + max(left, right)
dfs(root)
return maxSumPractice Binary Tree Maximum Path Sum and similar Trees problems with flashcards. Build pattern recognition through active recall.
Practice this problem