mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
28 lines
771 B
C++
28 lines
771 B
C++
/**
|
|
* Definition for a binary tree node.
|
|
* 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 rangeSumBST(TreeNode *root, int low, int high) {
|
|
if (root == nullptr) {
|
|
return 0;
|
|
}
|
|
|
|
int currentValue = 0;
|
|
if (low <= root->val && root->val <= high) {
|
|
currentValue = root->val;
|
|
}
|
|
|
|
return (currentValue + rangeSumBST(root->left, low, high) +
|
|
rangeSumBST(root->right, low, high));
|
|
}
|
|
};
|