-
Notifications
You must be signed in to change notification settings - Fork 481
/
1373.py
27 lines (23 loc) · 787 Bytes
/
1373.py
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
class Solution:
def maxSumBST(self, root: TreeNode) -> int:
maxv = 0
def dfs(root):
nonlocal maxv
if not root: return True, 0
res = [True, root.val]
if root.left:
left = dfs(root.left)
if left[0] and root.left.val < root.val:
res[1] += left[1]
else:
res[0] = False
if root.right:
right = dfs(root.right)
if right[0] and root.val < root.right.val:
res[1] += right[1]
else:
res[0] = False
if res[0]: maxv = max(maxv, res[1])
return res
dfs(root)
return maxv