我正在解决一些关于二叉树的问题,我陷入了这个问题。我使用python来解决这个问题我理解链接上给出的解决方案的逻辑,但是我的问题是,当SumOfLongRootToLeafPath()函数没有返回任何内容时,SumOfLongRootToLeafPathUtil(根)函数中的maxSum值是如何变化的?可变变化的原始值是如何变化的请帮助ps:请参考链接中给出的python代码
发布于 2021-06-10 07:20:57
传入SumOfLongRootToLeafPath函数的maxSum列表对象是可变的。因此,当它在该函数中更改时,SumOfLongRootToLeafPathUtil函数将看到对它的更改。因此,不需要返回值。
例如,显示列表的可变性质
def change_it(value):
value[0] = 12 # modify the list without creating a new one
value = [4]
print(value) # this will show [4]
change_it(value)
print(value) # this will show [12] as change_it has altered the value in the list如果元组用于maxSum而不是列表,那么有必要从SumOfLongRootToLeafPath返回结果,因为元组是不可变的。
例如,显示元组的不可变性质
def change_it(value):
value = (12, ) # can't modify the tuple, so create a new one
value = (4, )
print(value) # this will show (4,)
change_it(value)
print(value) # this will still show (4,) as change_it cannot modify the tuplehttps://stackoverflow.com/questions/67911298
复制相似问题