0

Write a function that checks if a given binary search tree contains a given value.

Im taking this little test on a website and its telling me that i passed the first test case but not the other two this is what i got class BinarySearchTree: Node = collections.namedtuple('Node', ['left', 'right', 'value']) @staticmethod def contains(root, value): if root.value == None: return False if root.value == value: return True elif root.value > value: return root.left.value == value return root.right.value == value n0 = BinarySearchTree.Node(value=0, left = None, right=None) n1 = BinarySearchTree.Node(value=1, left=n0, right=None) n3 = BinarySearchTree.Node(value=3, left=None, right=None) n2 = BinarySearchTree.Node(value=2, left=n1, right=n3) print(BinarySearchTree.contains(n2, 3)) OUTPUT: True

15th Apr 2018, 3:20 AM
fds fds
fds fds - avatar
2 Antworten
+ 1
You don't seem to be traversing the tree deeply enough. At least that's my opinion, as a python beginner. If you're not at a value, then you need to keep traversing more branches, until you exhaust your search.
15th Apr 2018, 4:21 AM
Emma
0
How would I accomplish that it seems that adding more if statements wouldn’t be the best idea to go deeper Any idea on how I can reach deeper
15th Apr 2018, 6:05 AM
fds fds
fds fds - avatar