-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumSumBSTinBinaryTree.py
More file actions
27 lines (25 loc) · 939 Bytes
/
maximumSumBSTinBinaryTree.py
File metadata and controls
27 lines (25 loc) · 939 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/
# Author: Miao Zhang
# Date: 2021-04-27
# 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 maxSumBST(self, root: TreeNode) -> int:
self.res = 0
def dfs(root):
if not root:
return [1e9, -1e9, 0, True]
lmin, lmax, lsum, lvalid = dfs(root.left)
rmin, rmax, rsum, rvalid = dfs(root.right)
valid = lvalid and rvalid and root.val > lmax and root.val < rmin
sums = lsum + rsum + root.val if valid else -1
self.res = max(self.res, sums)
return (min(lmin, root.val), max(rmax, root.val), sums, valid)
dfs(root)
return self.res