I have a problem in my program. Code as follows:
def getHeight(self,root):
#Write your code here
if not root:
return -1
if root.left:
i = self.getHeight(root.left) + 1
if root.right:
j = self.getHeight(root.right) + 1
return max(i, j)
Raises the error:
Traceback (most recent call last):
File "solution.py", line 37, in
height=myTree.getHeight(root)
File "solution.py", line 23, in getHeight
i = self.getHeight(root.left) + 1
File "solution.py", line 23, in getHeight
i = self.getHeight(root.left) + 1
File "solution.py", line 27, in getHeight
return max(i, j)
UnboundLocalError: local variable 'i' referenced before assignment
And if I add i, j = 0, 0, it will work well.
And following code:
if True:
i = 1 + 1
The code work well without initialization. Could someone please explain the difference?
write(i = 1 + 1) and another isread(return max(i, j)). Beforereading a variable, you need to create it first.