Graph Theory37 sections · 1633 units
Open in Course

LeetCode 124 Binary Tree Maximum Path Sum - Walkthrough

Example tree trace

Tree: root 10-10, left 99, right 2020 (left 1515, right 77). DFS leaf 1515: returns 1515. DFS leaf 77: returns 77. DFS node 2020: leftMax =15= 15, rightMax =7= 7. Updates maxPath =20+15+7=42= 20 + 15 + 7 = 42.

Returns 20+15=3520 + 15 = 35. DFS leaf 99: returns 99. DFS root 10-10: leftMax =9= 9, rightMax =35= 35. Updates maxPath =max(42,10+9+35)=max(42,34)=42= \max(42, -10 + 9 + 35) = \max(42, 34) = 42. Returns 10+35=25-10 + 35 = 25. Answer: 4242.