Created
October 6, 2019 00:53
-
-
Save sheldonrobinson/03ab2e3ef6128f31a749c44127a1571d to your computer and use it in GitHub Desktop.
CodeSignal Solution hasPathWithGivenSum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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