It can be solved with more clarity knowing that the gain from each left or right could be the max of that branch and zero to automatically exclude negative gains.
@SriHarshaBolisetti2 жыл бұрын
Thank you for your wonderful explanations!!
@crackfaang2 жыл бұрын
Thank you for your kind words! Make sure to subscribe so you don’t miss future videos
@SriHarshaBolisetti2 жыл бұрын
@@crackfaang already did long back :D
@rishab9293Ай бұрын
more clean code😀: class Solution: def maxPathSum(self, root: Optional[TreeNode]) -> int: self.res=float('-inf') self.helper(root) return self.res def helper(self,root): if not root: return 0 l=self.helper(root.left) r=self.helper(root.right) lmax=max(l,0) rmax=max(r,0) self.res=max(self.res,root.val+lmax+rmax) return root.val+max(lmax,rmax)