Skip to content
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 57 commits into from
Oct 7, 2024
Merged
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
6029b30
Added new algorithm
1227haran Oct 6, 2024
0d53dea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
2c98313
Updated changes
1227haran Oct 6, 2024
a5d835e
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 6, 2024
ec766f3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
822df1d
Updated filename
1227haran Oct 6, 2024
e8f1d4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
21dc6d9
Updated code
1227haran Oct 6, 2024
0aea0f2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
0a58155
Updated the code
1227haran Oct 6, 2024
421e92f
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 6, 2024
25a7ee6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
f42bca1
Updated code
1227haran Oct 6, 2024
c5b03b6
Updated merge conflicts
1227haran Oct 6, 2024
6179a8a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
3f3caff
Updated the code
1227haran Oct 6, 2024
ad04568
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
07e9c50
Updated code
1227haran Oct 6, 2024
3be3064
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 6, 2024
ff65aa3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
c2f438d
Updated the code
1227haran Oct 6, 2024
410e737
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 6, 2024
005205d
Updated code
1227haran Oct 6, 2024
b932346
Updated code
1227haran Oct 6, 2024
3aea631
Updated code
1227haran Oct 6, 2024
55dd178
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
e62a5fb
Updated the code
1227haran Oct 6, 2024
47ac98b
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 6, 2024
cf1e92a
Updated code
1227haran Oct 6, 2024
318fd22
Updated code
1227haran Oct 6, 2024
f1c5829
Updated code
1227haran Oct 6, 2024
cbc759b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
450f10a
Updated code
1227haran Oct 6, 2024
2baf142
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 6, 2024
906527f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
50aec67
Updated code
1227haran Oct 6, 2024
e10b4e3
updated
1227haran Oct 6, 2024
a946077
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 6, 2024
4852473
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
136cdec
Updated code
1227haran Oct 7, 2024
e0b6bf0
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 7, 2024
40d8653
Updated code
1227haran Oct 7, 2024
1534755
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
923a307
Updated code
1227haran Oct 7, 2024
a1d76c6
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 7, 2024
ac0709d
Updated code
1227haran Oct 7, 2024
e74d24e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
af00d57
Updated code
1227haran Oct 7, 2024
7d1f92a
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 7, 2024
bae1df8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
c2a62d9
Updated code
1227haran Oct 7, 2024
8df7748
Merge branch 'max_sum_binary_tree' of https://github.com/1227haran/Py…
1227haran Oct 7, 2024
cb2d96e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2024
b2ebccd
Apply suggestions from code review
cclauss Oct 7, 2024
d290a9c
Update maximum_sum_bst.py
cclauss Oct 7, 2024
95c9f15
def max_sum_bst(root: TreeNode | None) -> int:
cclauss Oct 7, 2024
619aef8
def solver(node: TreeNode | None) -> tuple[bool, int, int, int]:
cclauss Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions data_structures/binary_tree/maximum_sum_bst.py
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()