Skip to content

Commit ec766f3

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

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

data_structures/binary_tree/max_sum_BST.py

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

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

8+
def max_sum_bst(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
@@ -41,12 +41,11 @@ def solver(node) -> int:
4141
nonlocal ans
4242

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
is_left_valid, min_left, max_left, sum_left = solver(node.left)
4747
is_right_valid, min_right, max_right, sum_right = solver(node.right)
4848

49-
5049
if is_left_valid and is_right_valid and max_left < node.val < min_right:
5150
total_sum = sum_left + sum_right + node.val
5251
ans = max(ans, total_sum)

0 commit comments

Comments
 (0)