Skip to content

Commit ff1c19e

Browse files
committed
LeetCode: 938 solved
1 parent 7b642b1 commit ff1c19e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# https://leetcode.com/problems/range-sum-of-bst/solution/
2+
3+
# Definition for a binary tree node.
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
12+
13+
def solution(root):
14+
if not root:
15+
return 0
16+
17+
if low <= root.val <= high:
18+
return root.val + solution(root.left) + solution(root.right)
19+
elif root.val < low:
20+
return solution(root.right)
21+
elif root.val > high:
22+
return solution(root.left)
23+
24+
return solution(root)
25+

0 commit comments

Comments
 (0)