Skip to content

Commit 41ada53

Browse files
authored
Create Range_Sum_of_BST.py
1 parent 51bf86b commit 41ada53

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

leetcode/Range_Sum_of_BST.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
###### The main part is to understand the characteristics of Binary Search Tree,right >= node >= left
2+
# Definition for a binary tree node.
3+
# class TreeNode:
4+
# def __init__(self, val=0, left=None, right=None):
5+
# self.val = val
6+
# self.left = left
7+
# self.right = right
8+
class Solution:
9+
def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
10+
if not root: return 0
11+
if root.val > high: return self.rangeSumBST(root.left, low, high)
12+
if root.val < low: return self.rangeSumBST(root.right,low,high)
13+
return root.val + self.rangeSumBST(root.left, low, high) + self.rangeSumBST(root.right,low,high)
14+

0 commit comments

Comments
 (0)