Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #
  2. # Binary trees are already defined with this interface:
  3. # class Tree(object):
  4. # def __init__(self, x):
  5. # self.value = x
  6. # self.left = None
  7. # self.right = None
  8. def hasPathWithGivenSum(t, s):
  9. if t is None:
  10. if s==0:
  11. return True
  12. else:
  13. return False
  14. else:
  15. if t.left is not None and t.right is not None:
  16. return any([hasPathWithGivenSum(t.left, s-t.value), hasPathWithGivenSum(t.right, s-t.value)])
  17. elif t.left is not None:
  18. return hasPathWithGivenSum(t.left, s-t.value)
  19. elif t.right is not None:
  20. return hasPathWithGivenSum(t.right, s-t.value)
  21. else:
  22. if t.value==s:
  23. return True
  24. else:
  25. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement