Graph Theory37 sections · 1633 units
Open in Course

LeetCode 124 Binary Tree Maximum Path Sum - Core Idea

Path through node vs from node

There are two values to track:

1.1. Max path going down from vv (used for parent's calculation).

2.2. Max path through vv (candidate for global answer).

Path through vv = v+extleftMax+extrightMaxv + ext{leftMax} + ext{rightMax} (if both positive). Path from vv = v+max(extleftMax,extrightMax,0)v + max( ext{leftMax}, ext{rightMax}, 0). Return this to parent. The parent can only extend one branch, not both. If the parent used both branches, it would create a branching structure, not a path.