forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
30 lines (29 loc) · 751 Bytes
/
solution.cpp
File metadata and controls
30 lines (29 loc) · 751 Bytes
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int preOrder(TreeNode* root, int L, int R){
if(root == NULL)
return 0;
if(root->val >= L && root->val <= R){
return root->val + preOrder(root->left,L,R) + preOrder(root->right,L,R);
}
if(root->val < L){
return preOrder(root->right,L,R);
}
if(root->val > R){
return preOrder(root->left,L,R);
}
return 0;
}
int rangeSumBST(TreeNode* root, int L, int R) {
return preOrder(root, L, R);
}
};