Pseudocode:
maxPath := -infinity
function dfs(v)
if v is null then
return 0
leftMax := max(dfs(v.left), 0)
rightMax := max(dfs(v.right), 0)
maxPath := max(maxPath, v.val + leftMax + rightMax)
return v.val + max(leftMax, rightMax)
Call dfs(root), then return maxPath. Time: (visit each node once). Space: for recursion stack where is tree height. On balanced trees, . On skewed trees, .