Код: Выделить всё
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
balanced = [True]
def node_height(root):
if not root or not balanced[0]:
return 0
left_height = node_height(root.left)
right_height = node_height(root.right)
if abs(left_height - right_height) > 1:
balanced[0] = False
return 0
return 1 + max(left_height, right_height)
node_height(root)
return balanced[0]
Код: Выделить всё
balanced = Trueизменения сбалансированного[0] на сбалансированный в строках 6, 13 и 19, я получаю ошибку.
Код: Выделить всё
class solution:
def func():
a = 5
b = 7
c = True
def nested_func():
return (a + b, c)
return nested_func()
print('sum is:', func())
который показывает, что вложенные функции могут получать переменные вне себя.
Может кто-нибудь объяснить это?
Подробнее здесь: https://stackoverflow.com/questions/787 ... d-function