-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path124_binTreeMaxPathSun.py
More file actions
34 lines (24 loc) · 902 Bytes
/
Copy path124_binTreeMaxPathSun.py
File metadata and controls
34 lines (24 loc) · 902 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
30
31
32
33
34
# 124. Binary Tree Maximum Path Sum
# Neetcode: https://www.youtube.com/watch?v=Hr5cWUld4vU
# 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: Optional[TreeNode]) -> int:
res = [root.val]
def dfs(root):
if not root:
return 0
leftmax = dfs(root.left)
rightmax = dfs(root.right)
# Max path with splitting
left = max(leftmax,0)
right = max(rightmax,0)
res[0] = max(res[0], root.val+left+right)
# Max path without splitting
return root.val+max(left,right)
dfs(root)
return res[0]