LeetCodeDiary

A Diary for solving LeetCode problems

View on GitHub
class Solution:
    def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
        if not root:
            return 0
        if low < root.val < high:
            leftSum = self.rangeSumBST(root.left,low,high)
            rightSum = self.rangeSumBST(root.right,low,high)
            return root.val + leftSum + rightSum
        elif root.val <= low:
            rightSum = self.rangeSumBST(root.right,low,high)
            if root.val == low:
                return rightSum + root.val  
            else:
                return rightSum
        elif root.val >= high:
            leftSum = self.rangeSumBST(root.left,low,high)
            if root.val == high:
                return leftSum + root.val  
            else:
                return leftSum

树 简单 每日一题

做过的题目

https://leetcode-cn.com/problems/range-sum-of-bst/