Skip to content

Instantly share code, notes, and snippets.

@FrancoMuniz
Created January 18, 2020 06:12
Show Gist options
  • Save FrancoMuniz/73b122835b3c8312f8bc26ac52eb72bb to your computer and use it in GitHub Desktop.
Save FrancoMuniz/73b122835b3c8312f8bc26ac52eb72bb to your computer and use it in GitHub Desktop.
Search if number exists on Binary Tree
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