通过给定总和找到根到叶路径。代码通过字符串成功,列表失败

发布于 2025-02-06 16:30:49 字数 1058 浏览 1 评论 0原文

我正在看一个二进制树问题。目的是找到从根到叶的所有路径,以使路径上的值总和到给定的 k

为此,我以两种不同的方式编写了相同的算法:

  1. 第一个版本使用字符串参数a
  2. 第二版使用列表参数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:

  1. The first version uses a string parameter a.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2025-02-13 16:30:49

这两个代码段之间的区别是:

  • 在第一个版本中,其中a是字符串,您可以执行a+= str(root.data)+“” 。这将创建一个新的字符串并将其分配给本地变量a。此操作不会影响呼叫者作为参数传递的字符串。此外,字符串在Python中是不变的。

  • 在第二版中,其中a是列表,您可以执行a.append(root.data)。此不会创建一个新列表。它突变了呼叫者作为参数通过的列表。实际上,该代码仅具有一个列表。因此

因此,在第一个版本中,添加值的效果是局部的,并且不会影响呼叫者的字符串,在第二版中,效果也可以到达呼叫者,因为它们使用了 same same list 。

要在第二个代码版本中应用相同的local 原理,请使用以下方式将调用替换为附加

a = a + [root.data]

现在原始列表 a 未突变。而是创建了一个新列表,并将其分配回本地变量a。但是在这里,呼叫者的列表仍然是原始列表。现在创建了一个新列表。

The difference between these two code snippets is:

  • In the first version, where a is a string, you execute a+=str(root.data)+" ". This creates a new string and assigns it to the local variable a. 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 execute a.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 all append 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:

a = a + [root.data]

Now the original list a is not mutated. Instead a new list is created, and assigned back to the local variable a. But here, the caller's list is still the original list. There is now a new list created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文