Given a Binary Search Tree. The task is to find the minimum element in this given BST.
Example 1:
Input:
5
/ \
4 6
/ \
3 7
/
1
Output: 1
Example 2:
Input:
9
\
10
\
11
Output: 9
Your Task: The task is to complete the function minValue() which takes root as the argument and returns the minimum element of BST. If the tree is empty, there is no minimum element, so return -1 in that case.
My Code:
def minValue(root):
if root is None:
return -1
elif root.left is None:
return root.data
else:
minValue(root.left)
This code is giving me the output None with every testcase whereas if I change my minValue(root.left)
in the else condition to return minValue(root.left)
, I'm getting correct answer. Can anyone tell me the reason as to why this is happening?