Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Definition for a binary tree node.
- # class TreeNode:
- # def __init__(self, val=0, left=None, right=None):
- # self.val = val
- # self.left = left
- # self.right = right
- class Solution:
- def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
- # Base case, below leaf node, return 0
- if not root:
- return 0
- # Store subtree sums
- subtree_sums = self.rangeSumBST(root.left, low, high) + self.rangeSumBST(root.right, low, high)
- # Add the current node's value if the current node is within range
- if root.val >= low and root.val <= high:
- return subtree_sums + root.val
- # Return the range sum of the subtree rooted at the current node
- return subtree_sums
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement