Skip to content

Instantly share code, notes, and snippets.

@dionyziz
Created January 8, 2020 17:52
Show Gist options
  • Save dionyziz/98749a1fd4ed0247ea9c03a30e1901f8 to your computer and use it in GitHub Desktop.
Save dionyziz/98749a1fd4ed0247ea9c03a30e1901f8 to your computer and use it in GitHub Desktop.
import operator
from functools import partial, reduce
def flatten(node):
if not node:
return []
return flatten(node.left) + [node.val] + flatten(node.right)
def rangeSumBST(root, L, R):
return reduce(operator.add,
filter(partial(operator.ge, R),
filter(partial(operator.le, L), flatten(root))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment