-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRangeSumBST.cpp
30 lines (30 loc) · 912 Bytes
/
RangeSumBST.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<<<<<<< HEAD
class Solution {
public:
int rangeSumBST(TreeNode* root, int L, int R) {
if(!root)
return 0;
if(root->val >=L && root->val <=R)
return root->val+rangeSumBST(root->left,L,R)+rangeSumBST(root->right,L,R);
if(root->val < L)
return rangeSumBST(root->right,L,R);
if(root->val > R)
return rangeSumBST(root->left,L,R);
return 0;
}
=======
class Solution {
public:
int rangeSumBST(TreeNode* root, int L, int R) {
if(!root)
return 0;
if(root->val >=L && root->val <=R)
return root->val+rangeSumBST(root->left,L,R)+rangeSumBST(root->right,L,R);
if(root->val < L)
return rangeSumBST(root->right,L,R);
if(root->val > R)
return rangeSumBST(root->left,L,R);
return 0;
}
>>>>>>> 20843e6664516c6814c0e970cfafb680d978dfd5
};