Python 使用max-sum-can';不要比较问题

Python 使用max-sum-can';不要比较问题,python,binary,sum,max,Python,Binary,Sum,Max,我现在正在寻找最大和的根到叶路径。我的做法如下: def max_sum(root): _max = 0 find_max(root, _max, 0) return _max def find_max(node, max_sum, current_sum): if not node: return 0 current_sum += node.value if not node.left and not node.right:

我现在正在寻找最大和的根到叶路径。我的做法如下:

def max_sum(root):
    _max = 0
    find_max(root, _max, 0)
    return _max

def find_max(node, max_sum, current_sum):
    if not node:
        return 0
    current_sum += node.value
    if not node.left and not node.right:
        print(current_sum, max_sum, current_sum > max_sum)
        max_sum = max(max_sum, current_sum)
    if node.left:
        find_max(node.left, max_sum, current_sum)
    if node.right:
        find_max(node.right, max_sum, current_sum)
    current_sum -= node.value

class TreeNode():
    def __init__(self, _value):
        self.value = _value
        self.left, self.right, self.next = None, None, None

def main():
    root = TreeNode(1)
    root.left = TreeNode(7)
    root.right = TreeNode(9)
    root.left.left = TreeNode(4)
    root.left.right = TreeNode(5)
    root.right.left = TreeNode(2)
    root.right.right = TreeNode(7)

    print(max_sum(root))

    root = TreeNode(12)
    root.left = TreeNode(7)
    root.right = TreeNode(1)
    root.left.left = TreeNode(4)
    root.right.left = TreeNode(10)
    root.right.right = TreeNode(5)

    print(max_sum(root))

main()
输出:

12 0 True
13 0 True
12 0 True
17 0 True
0
23 0 True
23 0 True
18 0 True
0

Process finished with exit code 0
预期产出分别为17和23


我想确认为什么我的方法不能比较
max\u sum
current\u sum
?即使它在比较中返回true,但不会更新
max\u sum
。感谢您的帮助。

错误修复

这里有一种方法可以修复您的
find\u sum
函数-

def find_max(节点,当前_sum=0):
#空树
如果不是节点:
返回电流和
#分支机构
elif node.left或node.right:
下一个总和=当前总和+节点值
左=查找最大值(node.left,next_sum)
右=查找最大值(node.right,next\u sum)
返回最大值(左、右)
#叶子
其他:
返回当前值+节点值
t1=TreeNode\
( 1
,TreeNode(7,TreeNode(4),TreeNode(5))
,TreeNode(9,TreeNode(2),TreeNode(7))
)
t2=三烯醚\
( 12
,TreeNode(7,TreeNode(4),无)
,TreeNode(1,TreeNode(10),TreeNode(5))
)
打印(查找最大值(t1))
打印(查找最大值(t2))
17
23

查看流程

我们可以通过追踪其中一个例子来可视化计算过程,
find_max(t2)
-

12
/       \
7         1
/ \       / \
4无10 5
find_max(12,0)
/      \
7        1
/ \      / \
4无10 5
find_max(12,0)
/           \
max(find_max(7,12),find_max(1,12))
/ \                / \
4无10 5
find_max(12,0)
/                         \
max(find_max(7,12),find_max(1,12))
/              \                          /             \
max(find_max(4,19),find_max(None,19))max(find_max(10,13),find_max(5,13))
find_max(12,0)
/              \     
max(find_max(7,12),find_max(1,12))
/              \                /             \
最高(23,19)最高(23,18)
find_max(12,0)
/              \     
max(find_max(7,12),find_max(1,12))
|                                |
23                               23
find_max(12,0)
/            \     
最高(23,23)
find_max(12,0)
|
23
23

改进

但是我认为我们可以改进。就像我们在你的书中所做的那样,我们可以再次使用数学归纳法-

  • 如果输入树
    t
    为空,则返回空结果
  • (感应)
    t
    不是空的。如果存在子问题
    t.left
    t.right
    分支,则将
    t.value
    添加到累积结果
    r
    中,并在每个分支上重复出现
  • (归纳)
    t
    不为空,且
    t.left
    t.right
    均为空;已到达叶节点;将
    t.value
    添加到累计结果
    r
    中,并得出总和
  • def sum_分支(t,r=0):
    如果不是t:
    返回#(1)
    如果t.左或t.右:
    求和分支的收益率(t.左,r+t.值)#(2)
    sum_分支的收益率(t.右,r+t.值)
    其他:
    收益率r+t.值#(3)
    
    t1=TreeNode\
    ( 1
    ,TreeNode(7,TreeNode(4),TreeNode(5))
    ,TreeNode(9,TreeNode(2),TreeNode(7))
    )
    t2=三烯醚\
    ( 12
    ,TreeNode(7,TreeNode(4),无)
    ,TreeNode(1,TreeNode(10),TreeNode(5))
    )
    打印(最大值(总和分支(t1)))
    打印(最大值(总和分支(t2)))
    
    17
    23
    

    仿制药

    也许编写这个问题的一种更有趣的方法是先编写一个通用的
    路径
    函数-

    def路径(t,p=[]):
    如果不是t:
    返回#(1)
    如果t.左或t.右:
    路径的收益率(t.left,[*p,t.value])#(2)
    路径的收益率(t.right,[*p,t.value])
    其他:
    收益率[*p,t.value]#(3)
    
    然后我们可以将最大和问题作为泛型函数
    max
    sum
    路径的组合来解决-

    print(路径(t1)中x的最大和(x)))
    打印(路径(t2)中x的最大值(和(x)))
    
    17
    23
    
    尚未详细查看您的代码,但拥有一个函数和另一个同名变量是一个糟糕的想法。
    max\u sum
    \u max
    设置为0,然后从未更改。因此,返回0。谢谢,@turtle。我不确定哪个变量名与函数名冲突?谢谢@mkrieger1。你介意再解释一下吗?我想我的代码将要更新
    find\u max
    函数中的
    \u max
    。@QiangSuper:我的意思是
    max\u sum
    。正如另一个人所指出的,问题在于
    find\u max
    中的变量
    max\u sum
    仅是该函数的局部变量。更新其值不会更改
    \u max
    的值,谢谢您的帮助。你介意看一下我的密码吗?我关心的是为什么
    max\u sum
    语句没有更新
    max\u sum=max(max\u sum,current\u sum)
    ?问题在于
    find\u max(node.left,max\u sum,current\u sum)
    find\u max(node.right,max\u sum,current\u sum)
    我们不使用这些调用的返回值。另外,
    max_sum
    通过值传递,而不是通过引用传递。在你的另一个问题中,这种技术之所以有效,是因为
    []
    是pas