-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path938.java
More file actions
23 lines (22 loc) · 712 Bytes
/
938.java
File metadata and controls
23 lines (22 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Runtime: 0 ms, faster than 100.00% of Java online submissions for Range Sum of BST.
// Memory Usage: 45.5 MB, less than 96.74% of Java online submissions for Range Sum of BST.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
if (root == null) return 0;
int sum = (root.val >= L && root.val <= R) ? root.val : 0;
if (root.val < R)
sum += rangeSumBST(root.right, L, R);
if (root.val > L)
sum += rangeSumBST(root.left, L, R);
return sum;
}
}