938. Range Sum of BST

202408291746
tags: #tree #bst

var rangeSumBST = function(root, low, high) {
	if (root === null) {
		return 0;
	}
	let 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;
};

Reference

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