Skip to content

Commit 0d53dea

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 6029b30 commit 0d53dea

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

data_structures/binary_tree/max_sum_BST.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ def __init__(self, val=0, left=None, right=None):
44
self.left = left
55
self.right = right
66

7-
def maxSumBST(root: TreeNode) -> int:
87

8+
def maxSumBST(root: TreeNode) -> int:
99
"""
1010
The solution traverses a binary tree to find the maximum sum of
1111
keys in any subtree that is a Binary Search Tree (BST). It uses
@@ -39,30 +39,25 @@ def maxSumBST(root: TreeNode) -> int:
3939

4040
def solver(node):
4141
nonlocal ans
42-
42+
4343
if not node:
44-
return True, float('inf'), float('-inf'), 0 # Valid BST, min, max, sum
45-
44+
return True, float("inf"), float("-inf"), 0 # Valid BST, min, max, sum
45+
4646
isLeftValid, min_left, max_left, sum_left = solver(node.left)
4747
isRightValid, min_right, max_right, sum_right = solver(node.right)
4848

4949
if isLeftValid and isRightValid and max_left < node.val < min_right:
5050
total_sum = sum_left + sum_right + node.val
5151
ans = max(ans, total_sum)
5252
return True, min(min_left, node.val), max(max_right, node.val), total_sum
53-
53+
5454
return False, -1, -1, -1 # Not a valid BST
5555

5656
solver(root)
5757
return ans
58-
59-
if __name__ == "__main__":
60-
61-
import doctest
62-
doctest.testmod()
63-
64-
65-
6658

6759

60+
if __name__ == "__main__":
61+
import doctest
6862

63+
doctest.testmod()

0 commit comments

Comments
 (0)