通过给定总和找到根到叶路径。代码通过字符串成功,列表失败
我正在看一个二进制树问题。目的是找到从根到叶的所有路径,以使路径上的值总和到给定的 k 。
为此,我以两种不同的方式编写了相同的算法:
- 第一个版本使用字符串参数
a
。 - 第二版使用列表参数
a
。
他们应该给出相同的路径结果,但是第二版将打印出几乎包含树中所有值的路径。
版本1
def rootToLeafPathsSumToK(root, k, a):
if root == None :
return None
if root.left==None and root.right==None:
if root.data==k:
print(a+ str(root.data)+" ")
return
a+=str(root.data)+" "
rootToLeafPathsSumToK(root.left,k-root.data,a)
rootToLeafPathsSumToK(root.right,k-root.data,a)
输出:
5 6 2
5 7 1
版本2
def rootToLeafPathsSumToK(root, k, a):
if root == None :
return None
if root.left==None and root.right==None:
if root.data==k:
a.append(root.data)
print(a)
return
a.append(root.data)
rootToLeafPathsSumToK(root.left,k-root.data,a)
rootToLeafPathsSumToK(root.right,k-root.data,a)
I am looking at a binary tree problem. The goal is to find all path(s) from root to leaf, such that the values on the path sum up to a given k.
For that purpose I have written the same algorithm in two different ways:
- The first version uses a string parameter
a
. - The second version uses a list parameter
a
.
They are supposed to give the same path result, but the second version prints a path that includes almost all the values in the tree.
Version 1
def rootToLeafPathsSumToK(root, k, a):
if root == None :
return None
if root.left==None and root.right==None:
if root.data==k:
print(a+ str(root.data)+" ")
return
a+=str(root.data)+" "
rootToLeafPathsSumToK(root.left,k-root.data,a)
rootToLeafPathsSumToK(root.right,k-root.data,a)
output:
5 6 2
5 7 1
Version 2
def rootToLeafPathsSumToK(root, k, a):
if root == None :
return None
if root.left==None and root.right==None:
if root.data==k:
a.append(root.data)
print(a)
return
a.append(root.data)
rootToLeafPathsSumToK(root.left,k-root.data,a)
rootToLeafPathsSumToK(root.right,k-root.data,a)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两个代码段之间的区别是:
在第一个版本中,其中
a
是字符串,您可以执行a+= str(root.data)+“”
。这将创建一个新的字符串并将其分配给本地变量a
。此操作不会影响呼叫者作为参数传递的字符串。此外,字符串在Python中是不变的。在第二版中,其中
a
是列表,您可以执行a.append(root.data)
。此不会创建一个新列表。它突变了呼叫者作为参数通过的列表。实际上,该代码仅具有一个列表。因此因此,在第一个版本中,添加值的效果是局部的,并且不会影响呼叫者的字符串,在第二版中,效果也可以到达呼叫者,因为它们使用了 same same list 。
要在第二个代码版本中应用相同的local 原理,请使用以下方式将调用替换为
附加
:现在原始列表 a 未突变。而是创建了一个新列表,并将其分配回本地变量
a
。但是在这里,呼叫者的列表仍然是原始列表。现在创建了一个新列表。The difference between these two code snippets is:
In the first version, where
a
is a string, you executea+=str(root.data)+" "
. This creates a new string and assigns it to the local variablea
. This operation does not affect the string that the caller had passed as argument. Moreover, strings are immutable in Python.In the second version, where
a
is a list, you executea.append(root.data)
. This does not create a new list. It mutates the list that the caller had passed as argument. In fact, that code only has one list. So allappend
calls affect that single list.So, where in the first version, the effect of adding a value is local and does not impact the caller's string, in the second version the effect is reaching also to the caller, since they use the same list.
To apply the same local principle in the second code version, replace the call to
append
with this:Now the original list
a
is not mutated. Instead a new list is created, and assigned back to the local variablea
. But here, the caller's list is still the original list. There is now a new list created.