最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Using list to find path sum of binary tree - Stack Overflow

programmeradmin4浏览0评论
def path_sum(root,temp):

    print("root is",root)
    if root == None:
        return False
    temp.append(root.val)
    if root.left == None and root.right == None:
        
        
        if sum(temp) == targetSum:
            return True
        
    l=path_sum(root.left,temp)
    print("temp is",temp)
    r=path_sum(root.right,temp)
    temp.pop()
    return l or r

if my tree is as below:

 1

/ \
2 3
  /
  4

Using above code when I call path_sum(root.left,temp) recursively so when root is 2 it updates temp as [1,2] and then return after checking its sum with target sum and the pop operation runs after both left and right tree is traversed of a node but on running this code I am seeing that after node 2 is traversed it pops node 2 . I don't understand this because at path_sum(1.left,temp) it will return and update 'l' value then it should run for 'r' value and then pop should be called. Can someone explain please.

def path_sum(root,temp):

    print("root is",root)
    if root == None:
        return False
    temp.append(root.val)
    if root.left == None and root.right == None:
        
        
        if sum(temp) == targetSum:
            return True
        
    l=path_sum(root.left,temp)
    print("temp is",temp)
    r=path_sum(root.right,temp)
    temp.pop()
    return l or r

if my tree is as below:

 1

/ \
2 3
  /
  4

Using above code when I call path_sum(root.left,temp) recursively so when root is 2 it updates temp as [1,2] and then return after checking its sum with target sum and the pop operation runs after both left and right tree is traversed of a node but on running this code I am seeing that after node 2 is traversed it pops node 2 . I don't understand this because at path_sum(1.left,temp) it will return and update 'l' value then it should run for 'r' value and then pop should be called. Can someone explain please.

Share Improve this question edited Mar 6 at 18:25 data_geek asked Mar 6 at 18:21 data_geekdata_geek 11 bronze badge 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Mar 7 at 12:49
Add a comment  | 

1 Answer 1

Reset to default 1

The expectation from path_sum function seem to be that it leaves temp the same as it was originally called with. The return from the middle violates that expectation (as root's value is added to temp right before the return, but not removed afterwards.

Some ways to fix are - rewrite code to have single return at the end, or restore temp before every return, or have local temporary copy of temp in the function so you don't change the original one.

Note that this is not the ideal way to solve that leetcode problem (as it recalculates sum many times) but I recommend to get your code working first before rewriting.

发布评论

评论列表(0)

  1. 暂无评论