[英]Recursive Function: recursion with python
I have this Function: 我有这个功能:
def depth(self):
if self.root == None:
return 0
left = depth(self.left)
right = depth(self.right)
if left>right:
return left+1
else:
return right+1
But when I run it, the following error pops up: 但是当我运行它时,会弹出以下错误:
File "/Users/suryakannan/Documents/Python Programs/binaryTree.py", line 60, in depth
left = depth(self.left)
NameError: name 'depth' is not defined
So, what should I do to fix this function? 那么,我该怎么做才能修复此功能?
Since depth
is an instance method, you need to use the self
reference 由于depth
是实例方法,因此您需要使用self
引用
def depth(self):
if self.root == None:
return 0
left = self.depth(self.left)
right = self.depth(self.right)
if left>right:
return left+1
else:
return right+1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.