0

I am new to OOP in general and Python in particular and have trouble understanding the following code. It is used on a HackeRank problem. I dont get how check is used within its declaration. It is just supposed to return a Boolean.

    def check(root,mini,maxi):
        return(root==None or
        (check(root.left,mini,root.data) and 
        check(root.right,root.data,maxi)))

2 Answers2

1

You can see @helptaker's explanation to recursion. In this case, I'm assuming the function checks the left and right halves of the data unless it's empty. It will then take the results of those recursive calls and combine them.

So in your example, the base case would be when root==None, which is probably when the data gets small enough. Otherwise, this function would call itself on two smaller-sized problems. These two recursive calls would return their results, and the function would determine its return value based on those results.

In conclusion this function will break the problem down into smaller and smaller pieces and solve them each, combining the results.

Here's some resources on recursion:

(If you search "recursion" on Google, it will even show "Did you mean: recursion" as an example)

Community
  • 1
  • 1
abacles
  • 849
  • 1
  • 7
  • 15
0

Using a function within itself is called recursion.When a function is called within itself then it again calls itself into it's previously running call.This recursion pushes each recursive function call in a stack and completion of each function call results in popping of function call which is on top of the stack which happens to be the lastest call to function. So wheck check is called within itself , it will result in activation of another call to check and the value returned by this second call will be used in call which called it.It may happen Any number of times according to the conditions of the program . I hope this helps. Or you may read about recursion .

sopho-saksham
  • 171
  • 1
  • 11