<p>Given the <code>root</code> node of a binary search tree and two integers <code>low</code> and <code>high</code>, return <em>the sum of values of all nodes with a value in the <strong>inclusive</strong> range </em><code>[low, high]</code>.</p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 2 * 10^4]</code>.</li>
<li><code>1 <= Node.val <= 10^5</code></li>
<li><code>1 <= low <= high <= 10^5</code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
</ul>
from leetcode_py import TreeNodeclass Solution: # Time: O(n) # Space: O(h) where h is the tree height def range_sum_bst(self, root: TreeNode[int] | None, low: int, high: int) -> int: if root is None: return 0 if root.val < low: # Entire left subtree is below the range return self.range_sum_bst(root.right, low, high) if root.val > high: # Entire right subtree is above the range return self.range_sum_bst(root.left, low, high) return ( root.val + self.range_sum_bst(root.left, low, high) + self.range_sum_bst(root.right, low, high) )