Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 124 Binary Tree Maximum Path Sum - Problem Statement

Classic tree DP problem

Find the maximum sum path in a binary tree. A path can start and end at any nodes, but must follow edges. observation: at each node, the path either passes through it (joining left and right subtrees) or is entirely in one subtree.

Return two values: maxDownmaxDown = max sum path starting at this node and going down, maxPathmaxPath = max path in subtree. At node vv: maxDown=max(v.val,v.val+max(left.maxDown,right.maxDown))maxDown = \max(v.val, v.val + \max(left.maxDown, right.maxDown)). maxPath=max(left.maxPath,right.maxPath,v.val+left.maxDown+right.maxDown)maxPath = \max(left.maxPath, right.maxPath, v.val + left.maxDown + right.maxDown).