Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 822 Bytes

938_range_sum_of_bst.md

File metadata and controls

34 lines (26 loc) · 822 Bytes

[Easy] 938. Range Sum of BST

Question

[Easy] 938. Range Sum of BST

Thought

Code

# 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:
        def dfs(node: TreeNode):
            if node:
                if low <= node.val <= high:
                    self.result += node.val

                if node.val > low:
                    dfs(node.left)

                if node.val < high:
                    dfs(node.right)
                
        self.result = 0
        dfs(root)
        return self.result