diff options
author | terminaldweller <devi@terminaldweller.com> | 2024-01-08 05:28:20 +0000 |
---|---|---|
committer | terminaldweller <devi@terminaldweller.com> | 2024-01-08 05:28:20 +0000 |
commit | b1a505830a8e69644773f0595f62381d98a018ec (patch) | |
tree | 64c5392f8c160c75a4abef272f2cbceb6aca8859 | |
parent | 26121 (diff) | |
download | leetcode-b1a505830a8e69644773f0595f62381d98a018ec.tar.gz leetcode-b1a505830a8e69644773f0595f62381d98a018ec.zip |
938 py
-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() |