-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbinary-tree-maximum-path-sum.py
More file actions
29 lines (22 loc) · 990 Bytes
/
binary-tree-maximum-path-sum.py
File metadata and controls
29 lines (22 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
def dfs(node: TreeNode) -> (int, int):
max_sum, max_path = node.val, node.val
if node.left:
max_sum_left, max_path_left = dfs(node.left)
max_path = max(max_path, max_path_left + node.val)
max_sum = max(max_sum, max_sum_left, max_path)
if node.right:
max_sum_right, max_path_right = dfs(node.right)
max_path = max(max_path, max_path_right + node.val)
max_sum = max(max_sum, max_sum_right, max_path)
if node.left and node.right:
max_sum = max(max_sum, max_path_left + max_path_right + node.val)
return max_sum, max_path
return dfs(root)[0]