如何从函数 SumOfLongRootToLeafPath 返回值

How is the value returned from the function SumOfLongRootToLeafPath

我在解决一些关于二叉树的问题,但我卡在了这个问题中https://www.geeksforgeeks.org/sum-nodes-longest-path-root-leaf-node/ 我正在使用 python 来解决问题 我理解 link 上给出的解决方案的逻辑,但我的问题是当 SumOfLongRootToLeafPath() 函数未返回任何内容时,SumOfLongRootToLeafPathUtil(root) 函数中 maxSum 的值如何变化 变量的原始值如何变化请帮忙 ps:Please参考python中给出的代码link

传入 SumOfLongRootToLeafPath 函数的 maxSum 列表对象是可变的。因此,当它在该函数内发生更改时,SumOfLongRootToLeafPathUtil 函数将看到对其所做的更改。所以,没有必要return一个值。

例如显示列表的可变性质

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 而不是 List,则有必要 return 来自 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 tuple