Skip to content

Commit f9a3c8f

Browse files
committed
solve problem Range Sum Of Bst
1 parent f7284ab commit f9a3c8f

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ All solutions will be accepted!
311311
|8|[String To Integer Atoi](https://leetcode-cn.com/problems/string-to-integer-atoi/description/)|[java/py/js](./algorithms/StringToIntegerAtoi)|Medium|
312312
|299|[Bulls And Cows](https://leetcode-cn.com/problems/bulls-and-cows/description/)|[java/py/js](./algorithms/BullsAndCows)|Medium|
313313
|394|[Decode String](https://leetcode-cn.com/problems/decode-string/description/)|[java/py/js](./algorithms/DecodeString)|Medium|
314+
|938|[Range Sum Of Bst](https://leetcode-cn.com/problems/range-sum-of-bst/description/)|[java/py/js](./algorithms/RangeSumOfBst)|Medium|
314315

315316
# Database
316317
|#|Title|Solution|Difficulty|

algorithms/RangeSumOfBst/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Range Sum Of Bst
2+
We can solve this problem by recursive
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int rangeSumBST(TreeNode root, int L, int R) {
12+
if (root == null)
13+
return 0;
14+
else if (root.val < L)
15+
return rangeSumBST(root.right, L, R);
16+
else if (root.val > R)
17+
return rangeSumBST(root.left, L, R);
18+
else
19+
return root.val + rangeSumBST(root.right, L, R) + rangeSumBST(root.left, L, R);
20+
}
21+
}

algorithms/RangeSumOfBst/solution.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {number} L
11+
* @param {number} R
12+
* @return {number}
13+
*/
14+
var rangeSumBST = function(root, L, R) {
15+
if (!root)
16+
return 0
17+
else if (root.val < L)
18+
return rangeSumBST(root.right, L, R)
19+
else if (root.val > R)
20+
return rangeSumBST(root.left, L, R)
21+
else
22+
return root.val + rangeSumBST(root.right, L, R) + rangeSumBST(root.left, L, R)
23+
};

algorithms/RangeSumOfBst/solution.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def rangeSumBST(self, root, L, R):
10+
"""
11+
:type root: TreeNode
12+
:type L: int
13+
:type R: int
14+
:rtype: int
15+
"""
16+
if not root:
17+
return 0
18+
elif root.val < L:
19+
return self.rangeSumBST(root.right, L, R)
20+
elif root.val > R:
21+
return self.rangeSumBST(root.left, L, R)
22+
else:
23+
return root.val + self.rangeSumBST(root.left, L, R) + self.rangeSumBST(root.right, L, R)

0 commit comments

Comments
 (0)