-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added max_sum_bst.py #11832
Added max_sum_bst.py #11832
Changes from 6 commits
6029b30
0d53dea
2c98313
a5d835e
ec766f3
822df1d
e8f1d4e
21dc6d9
0aea0f2
0a58155
421e92f
25a7ee6
f42bca1
c5b03b6
6179a8a
3f3caff
ad04568
07e9c50
3be3064
ff65aa3
c2f438d
410e737
005205d
b932346
3aea631
55dd178
e62a5fb
47ac98b
cf1e92a
318fd22
f1c5829
cbc759b
450f10a
2baf142
906527f
50aec67
e10b4e3
a946077
4852473
136cdec
e0b6bf0
40d8653
1534755
923a307
a1d76c6
ac0709d
e74d24e
af00d57
7d1f92a
bae1df8
c2a62d9
8df7748
cb2d96e
b2ebccd
d290a9c
95c9f15
619aef8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
|
||
def max_sum_bst(root: TreeNode) -> int: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
The solution traverses a binary tree to find the maximum sum of | ||
keys in any subtree that is a Binary Search Tree (BST). It uses | ||
recursion to validate BST properties and calculates sums, returning | ||
the highest sum found among all valid BST subtrees. | ||
|
||
>>> t1 = TreeNode(4) | ||
>>> t1.left = TreeNode(3) | ||
>>> t1.left.left = TreeNode(1) | ||
>>> t1.left.right = TreeNode(2) | ||
>>> print(max_sum_bst(t1)) | ||
2 | ||
>>> t2 = TreeNode(-4) | ||
>>> t2.left = TreeNode(-2) | ||
>>> t2.right = TreeNode(-5) | ||
>>> print(max_sum_bst(t2)) | ||
0 | ||
>>> t3 = TreeNode(1) | ||
>>> t3.left = TreeNode(4) | ||
>>> t3.left.left = TreeNode(2) | ||
>>> t3.left.right = TreeNode(4) | ||
>>> t3.right = TreeNode(3) | ||
>>> t3.right.left = TreeNode(2) | ||
>>> t3.right.right = TreeNode(5) | ||
>>> t3.right.right.left = TreeNode(4) | ||
>>> t3.right.right.right = TreeNode(6) | ||
>>> print(max_sum_bst(t3)) | ||
20 | ||
""" | ||
ans = 0 | ||
|
||
def solver(node) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
nonlocal ans | ||
|
||
if not node: | ||
return True, float("inf"), float("-inf"), 0 # Valid BST, min, max, sum | ||
|
||
is_left_valid, min_left, max_left, sum_left = solver(node.left) | ||
is_right_valid, min_right, max_right, sum_right = solver(node.right) | ||
|
||
if is_left_valid and is_right_valid and max_left < node.val < min_right: | ||
total_sum = sum_left + sum_right + node.val | ||
ans = max(ans, total_sum) | ||
return True, min(min_left, node.val), max(max_right, node.val), total_sum | ||
|
||
return False, -1, -1, -1 # Not a valid BST | ||
|
||
solver(root) | ||
return ans | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
import doctest | ||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this a https://docs.python.org/3/library/dataclasses.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_tree_traversals.py#L10 for a working example.