Skip to content

Commit a5d835e

Browse files
committed
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Python into max_sum_binary_tree
2 parents 2c98313 + 0d53dea commit a5d835e

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

data_structures/binary_tree/max_sum_BST.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,26 @@ def max_sum_bst(root: TreeNode) -> int:
3939

4040
def solver(node) -> int:
4141
nonlocal ans
42-
42+
4343
if not node:
4444
return True, float('inf'), float('-inf'), 0 # Valid BST, min, max, sum
4545

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+
4950
if is_left_valid and is_right_valid and max_left < node.val < min_right:
5051
total_sum = sum_left + sum_right + node.val
5152
ans = max(ans, total_sum)
5253
return True, min(min_left, node.val), max(max_right, node.val), total_sum
53-
54+
5455
return False, -1, -1, -1 # Not a valid BST
5556

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

6760

61+
if __name__ == "__main__":
62+
import doctest
6863

64+
doctest.testmod()

0 commit comments

Comments
 (0)