Krononberg

leetcode) 938. Range Sum of BST c++ 본문

개발 로그/알고리즘

leetcode) 938. Range Sum of BST c++

k._. 2021. 9. 23. 09:48

 

/**
 * 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 sum =0;
    int rangeSumBST(TreeNode* root, int low, int high) {
        if(root!=nullptr){
            if(root->val >=low && root->val <=high) sum += root->val;
            rangeSumBST(root->left,low,high);
            rangeSumBST(root->right,low,high);
        }
        return sum;
    }
};