/**
 @author Farheen Bano
  
 References-
 https://leetcode.com/problems/range-sum-of-bst/
 https://www.geeksforgeeks.org/count-bst-nodes-that-are-in-a-given-range/
*/

import java.io.*;
import java.util.*;
import java.lang.*;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int sum=0;
    public int rangeSumBST(TreeNode root, int low, int high) {
        if(root==null){
            return -1;
        }
        traversal(root,low,high);
        return sum;        
    }
    
    public void traversal(TreeNode root, int low, int high){
        if(root==null){
            return;
        }
        if(root.val>=low && root.val<=high)
            sum+=root.val;
        rangeSumBST(root.left,low,high);
        rangeSumBST(root.right,low,high);
    }
}
