# Python: Nested Function Problem

• This recursive solution works.

``````def isBalanced(self, root):
dep, ba = self.getDepth(root)
return ba
def getDepth(self,root):
if not root: return 0, True
ldep, lba = self.getDepth(root.left)
if not lba: return 0, False
rdep, rba = self.getDepth(root.right)
if not rba: return 0, False
if abs(ldep-rdep)>1: return 0, False
else: return max(ldep,rdep)+1, True
``````

Why using nested function does not work? The solution below gets a run-time error.

``````def isBalanced(self, root):
def getDepth(root):
if not root: return 0, True
ldep, lba = getDepth(root.left)
if not lba: return 0, False
rdep, rba = getDepth(root.right)
if not rba: return 0, False
if abs(ldep-rdep)>1: return 0, False
else: return max(ldep,rdep)+1, True
dep, ba = getDepth(root)
return ba``````

• You have to consider the interface between you code and test framework

• Yes. I meet the same problem and I still do not know the reason.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.