Created
January 18, 2020 06:12
-
-
Save FrancoMuniz/73b122835b3c8312f8bc26ac52eb72bb to your computer and use it in GitHub Desktop.
Search if number exists on Binary Tree
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
import collections | |
Node = collections.namedtuple('Node', ['left', 'right', 'value']) | |
def contains(currentNode, value): | |
if value == currentNode.value: | |
return(True) | |
else: | |
if not currentNode.right and value > currentNode.value: | |
return(False) | |
elif not currentNode.left and value < currentNode.value: | |
return(False) | |
elif value > currentNode.value: | |
return contains(currentNode.right, value) | |
elif value < currentNode.value: | |
return contains(currentNode.left, value) | |
n1 = Node(value=1, left=None, right=None) | |
n3 = Node(value=3, left=None, right=None) | |
n2 = Node(value=2, left=n1, right=n3) | |
print(contains(n2, 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment