938. 二叉搜索树的范围和 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
int sum=0;
public:
int rangeSumBST(TreeNode* root, int low, int high) {
if(root==nullptr) return 0;
fun(root,low,high);
return sum;
}
void fun(TreeNode*root,int low,int high){
if(root==nullptr) return;
fun(root->left,low,high);
if(root->val>=low&&root->val<=high) sum+=root->val;
fun(root->right,low,high);
}
};