File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ My solutions to some problems from [LeetCode](https://leetcode.com/problemset/al
52
52
| 690 | [ Employee Importance] [ 690 ] | [ Python] [ 690a ] | Easy |
53
53
| 700 | [ Search in a Binary Search Tree] [ 700 ] | [ Python] [ 700a ] | Easy |
54
54
| 788 | [ Rotated Digits] [ 788 ] | [ Python] [ 788a ] | Easy |
55
+ | 938 | [ Range Sum of BST] [ 938 ] | [ Python] [ 938a ] | Easy |
55
56
| 1029 | [ Two City Scheduling] [ 1029 ] | [ Python] [ 1029a ] | Easy |
56
57
| 1137 | [ N-th Tribonacci Number] [ 1137 ] | [ Java] [ 1137a ] | Easy |
57
58
| 1275 | [ Find Winner on a Tic Tac Toe Game] [ 1275 ] | [ Python] [ 1275a ] | Easy |
@@ -153,6 +154,8 @@ My solutions to some problems from [LeetCode](https://leetcode.com/problemset/al
153
154
[ 700a ] : ./src/searchBST/search.py
154
155
[ 788 ] : https://leetcode.com/problems/rotated-digits/
155
156
[ 788a ] : ./src/rotatedDigits/dig.py
157
+ [ 938 ] : https://leetcode.com/problems/range-sum-of-bst/
158
+ [ 938a ] : ./src/rangeSumBST/sum.py
156
159
[ 1029 ] : https://leetcode.com/problems/two-city-scheduling/
157
160
[ 1029a ] : ./src/twoCityScheduling/sched.py
158
161
[ 1137 ] : https://leetcode.com/problems/n-th-tribonacci-number/
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ class TreeNode :
3
+ def __init__ (self , val = 0 , left = None , right = None ):
4
+ self .val = val
5
+ self .left = left
6
+ self .right = right
7
+
8
+
9
+ class Solution :
10
+ def rangeSumBST (self , root : TreeNode , L : int , R : int ) -> int :
11
+ if not root :
12
+ return 0
13
+ total = 0
14
+ # if node value is within range, add to total
15
+ if L <= root .val <= R :
16
+ total += root .val
17
+ # repeat process for sub-trees
18
+ total += self .rangeSumBST (root .left , L , R )
19
+ total += self .rangeSumBST (root .right , L , R )
20
+ return total
You can’t perform that action at this time.
0 commit comments