tree December 7, 2022

Range sum of bst

Time O(n) Space O(n) Open original problem

We will traverse the tree with DFS and add the value to the sum if it is in the range.

# 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: Optional[TreeNode], low: int, high: int) -> int:
        self.sum = 0
        def dfs(root):
            if not root:
                return
            if low <= root.val <= high:
                self.sum += root.val
            dfs(root.left)
            dfs(root.right)
        dfs(root)
        return self.sum

Time complexity: O(n)
Space complexity: O(n)