I'm trying to implement a recursive method to calculate the height of a binary tree. Here is the "height"-code:
def HeightOfTree(self):
if self.root is None:
return 0
else:
ans=self._HeightOfTree(self.root)
return ans
def _HeightOfTree(self,currentNode):
if currentNode.hasleftChild():
lheight=1+self._HeightOfTree(currentNode.leftChild)
if currentNode.hasrightChild():
rheight=1+self._HeightOfTree(currentNode.rightChild)
if lheight > rheight:
return (lheight+1)
else:
return (rheight+1)
When I try to call the function, I get the following error msg:
UnboundLocalError: local variable 'lheight' referenced before assignment
How can I fix this problem ?