diff options
Diffstat (limited to '938')
-rwxr-xr-x | 938/main.py | 35 |
1 files changed, 35 insertions, 0 deletions
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() |