1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/cpp/range-sum-of-bst.cpp
Matej Focko 30202920b0
cpp: add “938. Range Sum of BST”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-01-08 11:50:46 +01:00

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));
}
};