# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def maxPathSum(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def DFS(root):
if not root: return 0
left_part = DFS(root.left)
right_part = DFS(root.right)
self.ans = max(self.ans, left_part + root.val + right_part)
return max(0, root.val, root.val + left_part, root.val + right_part)
self.ans = root.val
DFS(root)
return self.ans