"Sum of values in binary tree greater than or equal to given value"

Given a binary tree and a value, return the sum of all values in the tree that are greater than or equal to the given value.

Problem

Given a binary tree, return the sum of all values in the tree that are greater than or equal to the given value.
Example input:
root = [10,5,15,3,7,null,18], value = 7
Output:
32

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:
        if root is None:
            return 0
        if root.val < L:
            return self.rangeSumBST(root.right, L, R)
        if root.val > R:
            return self.rangeSumBST(root.left, L, R)
        return root.val + self.rangeSumBST(root.left, L, R) + self.rangeSumBST(root.right, L, R)

A.I. Evaluation of the Solution

The candidate's solution is complete and solves the problem. The approach is to recursively traverse the tree, keeping track of the sum of values that are greater than or equal to the given value. This is a good approach and is implemented correctly.

Evaluated at: 2022-11-07 00:15:21