0

I was working on a problem on Binary Search Tree where I had to find the minimum value of the tree. Here is the link for the problem. Please visit to get a clear idea about the problem.

https://practice.geeksforgeeks.org/problems/minimum-element-in-bst/1

Below is my code:

def minValue(node):
  current=node
  if current.left==None:
      return current.data
  else:
      minValue(current.left)

Why is this not returning only minimum value but also None along with it in few cases?

HBK8396
  • 481
  • 1
  • 5
  • 9
  • Because you don't print anything. – mapf May 08 '20 at 07:30
  • Have you gone through the link I mentioned in the problem? It just needs me to return and not print. The engine prints the return value. Can you please paste this code and run it in the link mentioned in question. You will get a better Idea. – HBK8396 May 08 '20 at 07:47
  • I cannot access the code as I don't have an account on the page you linked. But more importantly, please understand that it is your task as the person who asks the question, to provide an [MWE](https://stackoverflow.com/help/minimal-reproducible-example) that includes all the relevant parts of the code, so that anyone can run it from the get-go. – mapf May 08 '20 at 07:53
  • Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – user2390182 May 08 '20 at 08:06
  • Yes Sir, Now the code works perfectly fine. Thank you very much – HBK8396 May 08 '20 at 08:18
  • But I have a return statement in 4th line of the code right, why do I need to return it in the last line? When the code reaches the node with minimum value, the return statement on 4th line would execute right? Please correct me if I am wrong. Thanks – HBK8396 May 08 '20 at 08:21

1 Answers1

0

Return the recursive call:

# ....
else:
    return minValue(current.left)

Without return, the function implicitly returns None in the else case.

user2390182
  • 72,016
  • 6
  • 67
  • 89