```
class Solution {
func maxPathSum(_ root: TreeNode?) -> Int {
var result = Int.min
DFS(root, &result)
return result
}
private func DFS(_ root: TreeNode?, _ result: inout Int) -> Int {
guard let root = root else {
return 0
}
let left = max(0, DFS(root.left, &result))
let right = max(0, DFS(root.right, &result))
result = max(result, left + right + root.val)
return max(left, right) + root.val
}
}
```