Leetcode 938: Range Sum of BST

Question: https://leetcode.com/problems/range-sum-of-bst/description/

class Solution {
    int result = 0;
    public int rangeSumBST(TreeNode root, int low, int high) {
        traverseTree(root, low, high);
        return result;
    }
    
    private void traverseTree(TreeNode root, int low, int high) {
        if (root == null) {
            return;
        }
        if (root.val >= low && root.val <= high) {
            result = root.val + result;
        }
        traverseTree(root.left, low, high);
        traverseTree(root.right, low, high);
    }
} 

Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close