75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

938. Range Sum of BST

Traversal Approaches

BST Traversal

class Solution {
public:
    int rangeSumBST(TreeNode* root, int low, int high)
    {
        if (root == NULL) return 0;
        int sum = 0;
        if (root->val >= low && root->val <= high) sum += root->val;
        if (root->val >= low) sum += rangeSumBST(root->left, low, high);
        if (root->val <= high) sum += rangeSumBST(root->right, low, high);
        return sum;
    }
};