Skip to content

Instantly share code, notes, and snippets.

@farsialgorithm
Created December 4, 2018 03:58
Show Gist options
  • Select an option

  • Save farsialgorithm/33dfb06d90398eff8d8de43cf16dad97 to your computer and use it in GitHub Desktop.

Select an option

Save farsialgorithm/33dfb06d90398eff8d8de43cf16dad97 to your computer and use it in GitHub Desktop.
from collections import deque
def rangeSumBST(root, L, R):
s = 0
queue = deque([root])
while queue:
c = queue.popleft()
if c:
if R<c.val:
queue.append(c.left)
elif c.val<L:
queue.append(c.right)
else:
s += c.val
queue.append(c.left)
queue.append(c.right)
return s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment