Skip to content

Instantly share code, notes, and snippets.

@sheldonrobinson
Created October 6, 2019 00:53
Show Gist options
  • Save sheldonrobinson/03ab2e3ef6128f31a749c44127a1571d to your computer and use it in GitHub Desktop.
Save sheldonrobinson/03ab2e3ef6128f31a749c44127a1571d to your computer and use it in GitHub Desktop.
CodeSignal Solution hasPathWithGivenSum
#
# Binary trees are already defined with this interface:
# class Tree(object):
# def __init__(self, x):
# self.value = x
# self.left = None
# self.right = None
def hasPathWithGivenSum(t, s):
if t is None:
if s==0:
return True
else:
return False
else:
if t.left is not None and t.right is not None:
return any([hasPathWithGivenSum(t.left, s-t.value), hasPathWithGivenSum(t.right, s-t.value)])
elif t.left is not None:
return hasPathWithGivenSum(t.left, s-t.value)
elif t.right is not None:
return hasPathWithGivenSum(t.right, s-t.value)
else:
if t.value==s:
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment