I am trying to implement a python code to find the depth of a binary tree. I have successfully implemented the C++ version but when I implement the same code in python it is giving a different answer in Leetcode. C++ version:
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root) return 0;
int l=maxDepth(root->left);
int r=maxDepth(root->right);
return 1 + max(l, r);
}
Python version:
class Solution(object):
def maxDepth(self, root):
if root is None:
return 0
self.left=self.maxDepth(root.left)
self.right=self.maxDepth(root.right)
return max(self.left,self.right) +1
Is there any fundamental difference in how recursive calls are made in Python and C++. My python code fails for the following case:[1,2,3,4,5]