Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].

&nbsp;
Example 1:

Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32
Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.


Example 2:

Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
Output: 23
Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 2 * 104].
	1 &lt;= Node.val &lt;= 105
	1 &lt;= low &lt;= high &lt;= 105
	All Node.val are unique.


class Solution:
    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
    
        res = 0
        
        def dfs(node):
            nonlocal res
            
            if not node:
                return
            
            if node.val >= low and node.val <= high:
                
                res += node.val
                
            dfs(node.left)
            dfs(node.right)
            
        dfs(root)
        
        return res
class Solution {
    private int sum = 0;
    public int rangeSumBST(TreeNode root, int low, int high) {
        dfs(root, low, high);
        return sum;
    }
    
    public void dfs(TreeNode root, int low, int high){
        if(root == null) return;
        
        if(root.val < low) dfs(root.right, low, high);
        else if(root.val > high) dfs(root.left, low, high);
        
        if(root.val >= low && root.val <= high) {
            sum += root.val;
            dfs(root.left, low, high);
            dfs(root.right, low, high);
        }
        
    }
}
class Solution {
public:

    int solve(TreeNode* root, int low, int high){
        if(root == NULL)
            return 0;

        int sum = 0;
        if(low <= root->val && root->val <= high){
            sum = root->val;
        }

        return sum + solve(root->left, low, high) + solve(root->right, low, high);
    }

    int rangeSumBST(TreeNode* root, int low, int high) {

        return solve(root, low, high);
    }
};
var rangeSumBST = function(root, low, high) {
    let sum = 0;
    const summer = (root) => {
        if(!root) {
            return;
        }

        sum = root.val >= low && root.val <= high ? root.val + sum : sum;

        summer(root.left)
        summer(root.right)
    }

    summer(root);

    return sum;
};
Range Sum of BST