Advertisement
smj007

Range Sum of BST

Aug 12th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. class Solution:
  2.     def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
  3.  
  4.         if not root:
  5.             return 0
  6.  
  7.         count = 0
  8.  
  9.         # if root.val <= low: won't work here
  10.         if root.val < low:
  11.             count += self.rangeSumBST(root.right, low, high)
  12.         # if root.val >= high: won't work here
  13.         elif root.val > high:
  14.             count += self.rangeSumBST(root.left, low, high)
  15.         else:
  16.             count += root.val
  17.             count += self.rangeSumBST(root.left, low, high)
  18.             count += self.rangeSumBST(root.right, low, high)
  19.  
  20.         return count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement