Thursday, February 18, 2021

Leetcode Everyday: 938. Range Sum of BST. Easy

public class Solution {
    public int RangeSumBST(TreeNode root, int low, int high) {
        int sum = low <= root.val && root.val <= high ? root.val : 0;
        
        if (root.left != null) {
            sum += RangeSumBST(root.left, low, high);
        }
        if (root.right != null) {
            sum += RangeSumBST(root.right, low, high);
        }
        
        return sum;
    }
}
Source: https://leetcode.com/problems/range-sum-of-bst/

No comments:

Post a Comment