-
-
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
Merged
Merged
Added max_sum_bst.py #11832
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
6029b30
Added new algorithm
1227haran 0d53dea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2c98313
Updated changes
1227haran a5d835e
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran ec766f3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 822df1d
Updated filename
1227haran e8f1d4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 21dc6d9
Updated code
1227haran 0aea0f2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0a58155
Updated the code
1227haran 421e92f
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran 25a7ee6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f42bca1
Updated code
1227haran c5b03b6
Updated merge conflicts
1227haran 6179a8a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3f3caff
Updated the code
1227haran ad04568
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 07e9c50
Updated code
1227haran 3be3064
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran ff65aa3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c2f438d
Updated the code
1227haran 410e737
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran 005205d
Updated code
1227haran b932346
Updated code
1227haran 3aea631
Updated code
1227haran 55dd178
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e62a5fb
Updated the code
1227haran 47ac98b
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran cf1e92a
Updated code
1227haran 318fd22
Updated code
1227haran f1c5829
Updated code
1227haran cbc759b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 450f10a
Updated code
1227haran 2baf142
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran 906527f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 50aec67
Updated code
1227haran e10b4e3
updated
1227haran a946077
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran 4852473
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 136cdec
Updated code
1227haran e0b6bf0
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran 40d8653
Updated code
1227haran 1534755
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 923a307
Updated code
1227haran a1d76c6
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran ac0709d
Updated code
1227haran e74d24e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] af00d57
Updated code
1227haran 7d1f92a
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran bae1df8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c2a62d9
Updated code
1227haran 8df7748
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran cb2d96e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b2ebccd
Apply suggestions from code review
cclauss d290a9c
Update maximum_sum_bst.py
cclauss 95c9f15
def max_sum_bst(root: TreeNode | None) -> int:
cclauss 619aef8
def solver(node: TreeNode | None) -> tuple[bool, int, int, int]:
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from dataclasses import dataclass | ||
|
||
INT_MIN = -sys.maxsize + 1 | ||
INT_MAX = sys.maxsize - 1 | ||
|
||
|
||
@dataclass | ||
class TreeNode: | ||
val: int = 0 | ||
left: TreeNode | None = None | ||
right: TreeNode | None = None | ||
|
||
|
||
def max_sum_bst(root: TreeNode | None) -> int: | ||
""" | ||
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: int = 0 | ||
|
||
def solver(node: TreeNode | None) -> tuple[bool, int, int, int]: | ||
""" | ||
Returns the maximum sum by making recursive calls | ||
>>> t1 = TreeNode(1) | ||
>>> print(solver(t1)) | ||
1 | ||
""" | ||
nonlocal ans | ||
|
||
if not node: | ||
return True, INT_MAX, INT_MIN, 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.