Here's a code for the program using recursion, "Given an integer n, return true if it is a power of three. Otherwise, return false."
class Solution:
def isPowerOfThree(self, n: int) -> bool:
if(n>0):
if(n == 1 or n == 3): return True
elif(n%3 != 0): return False
else: self.isPowerOfThree(n/3)
else: return False
This code when run in vs code passes all the test cases, while when run in Leetcode fails the test case of "27".
How is this possible? or does Leetcode compiler behave differently?
There is the missing return statement
else:
return self.isPowerOfThree(n // 3)
without this it can be return as None
, it can lead to recursive calls not returning the boolean value.