1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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()
|