I am trying to search for a value in a BST. The below code works perfectly.
def search(root, value):
if root is None:
print(“Not found.”)
return None
if root.data == value:
print(“Found”)
return root
if root.data < value:
return search(root.right, value)
return search(root.left, value)
Output for the above is “Found”
But if I remove “return” and simply call search(root.right, value) - without “return” keyword I get the following output.
Found.
Not found.
Not found.
Not found.
Not found.
Not found.
Code for the above is:
def search(root, value):
if root is None:
print(“Not found.”)
return
if root.data == value:
print(“Found.”)
return root
if root.data < value:
search(root.right, value)
search(root.left, value)
Thank you.