-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-RangeSum.cpp
34 lines (33 loc) · 962 Bytes
/
08-RangeSum.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
31
32
33
34
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
int solve(TreeNode* node,int low,int high)
{
int x1=0,x2=0,x3=0;
if(node==NULL)
return 0;
if(node->val < low)
x1 = solve(node->right,low,high);
if(node->val > high)
x2 = solve(node->left,low,high);
if(node->val >= low && node->val<=high)
{
x1 = solve(node->left,low,high);
x2 = solve(node->right,low,high);
x3 = node->val;
}
return x1+x2+x3;
}
int rangeSumBST(TreeNode* root, int low, int high) {
return solve(root,low,high);
}
};