publicintmaxPathSum2(TreeNode root) {if (root ==null) {returnInteger.MIN_VALUE; }int left =maxPathSum2(root.left);int right =maxPathSum2(root.right);// if number is nagtive, ignorereturnroot.val+Math.max(0,Math.max(left, right));}
124 Binary Tree Maximum Path Sum - any to any
Any node to any node
ResultType: rootPath, maxPath
defmaxPathSum(self,root: TreeNode) ->int: self.max =float('-inf') self.dfs(root)return self.maxdefdfs(self,node):ifnot node:return0 left = self.dfs(node.left) right = self.dfs(node.right) self.max =max(self.max, node.val + left + right)returnmax(0, node.val +max(left, right))