My Blog List

[Leetcode Solution] Binary Tree Maximum Path

Analysis

  • Classical dynamic programming problem
  • Let f[p] denote the max sum of a path starting from node p and ends at the node on the p's sub tree. Then 
    • f[p]=max(f[p->left], f[p->right], 0) + p->val
  • Then the max sum path is the node p with max sum of p->val+f[p->left]+f[p->right]

Note

Code