1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00

cpp: add “938. Range Sum of BST”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-01-08 11:50:46 +01:00
parent 20632b1d7c
commit 30202920b0
Signed by: mfocko
GPG key ID: 7C47D46246790496

28
cpp/range-sum-of-bst.cpp Normal file
View file

@ -0,0 +1,28 @@
/**
* 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));
}
};