Skip to content

Added max_sum_bst.py #11832

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

Merged
merged 57 commits into from
Oct 7, 2024
Merged
Changes from 1 commit
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
68 changes: 68 additions & 0 deletions data_structures/binary_tree/max_sum_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class TreeNode:

Check failure on line 1 in data_structures/binary_tree/max_sum_BST.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/binary_tree/max_sum_BST.py:1:1: N999 Invalid module name: 'max_sum_BST'
def __init__(self, val=0, left=None, right=None):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: val

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

self.val = val
self.left = left
self.right = right

def maxSumBST(root: TreeNode) -> int:

Check failure on line 7 in data_structures/binary_tree/max_sum_BST.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/binary_tree/max_sum_BST.py:7:5: N802 Function name `maxSumBST` should be lowercase

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: maxSumBST


"""
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(maxSumBST(t1))
2
>>> t2 = TreeNode(-4)
>>> t2.left = TreeNode(-2)
>>> t2.right = TreeNode(-5)
>>> print(maxSumBST(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(maxSumBST(t3))
20
"""
ans = 0

def solver(node):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: solver. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/max_sum_BST.py, please provide doctest for the function solver

Please provide type hint for the parameter: node

nonlocal ans

Check failure on line 42 in data_structures/binary_tree/max_sum_BST.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/binary_tree/max_sum_BST.py:42:1: W293 Blank line contains whitespace
if not node:
return True, float('inf'), float('-inf'), 0 # Valid BST, min, max, sum

Check failure on line 45 in data_structures/binary_tree/max_sum_BST.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/binary_tree/max_sum_BST.py:45:1: W293 Blank line contains whitespace
isLeftValid, min_left, max_left, sum_left = solver(node.left)

Check failure on line 46 in data_structures/binary_tree/max_sum_BST.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/binary_tree/max_sum_BST.py:46:9: N806 Variable `isLeftValid` in function should be lowercase

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: isLeftValid

isRightValid, min_right, max_right, sum_right = solver(node.right)

Check failure on line 47 in data_structures/binary_tree/max_sum_BST.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/binary_tree/max_sum_BST.py:47:9: N806 Variable `isRightValid` in function should be lowercase

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: isRightValid


if isLeftValid and isRightValid 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

Check failure on line 53 in data_structures/binary_tree/max_sum_BST.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/binary_tree/max_sum_BST.py:53:1: W293 Blank line contains whitespace
return False, -1, -1, -1 # Not a valid BST

solver(root)
return ans

Check failure on line 58 in data_structures/binary_tree/max_sum_BST.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/binary_tree/max_sum_BST.py:58:1: W293 Blank line contains whitespace
if __name__ == "__main__":

import doctest
doctest.testmod()






Loading