From b1a505830a8e69644773f0595f62381d98a018ec Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Mon, 8 Jan 2024 00:28:20 -0500 Subject: 938 py --- 938/main.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 938/main.py (limited to '938') diff --git a/938/main.py b/938/main.py new file mode 100755 index 0000000..0728b21 --- /dev/null +++ b/938/main.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + import typing + + def rangeSumBST(self, root: typing.Optional[TreeNode], low: int, high: int) -> int: + if root is None: + return 0 + + if root.val < low or root.val > high: + return self.rangeSumBST(root.left, low, high) + self.rangeSumBST( + root.right, low, high + ) + return ( + root.val + + self.rangeSumBST(root.left, low, high) + + self.rangeSumBST(root.right, low, high) + ) + + +def main() -> None: + solution = Solution() + print(solution.rangeSumBST()) + + +if __name__ == "__main__": + main() -- cgit v1.2.3