[英]Sum of binary tree leaves' values
I wrote this code and when I use print I see that I get the leaves.我写了这段代码,当我使用 print 时,我看到我得到了叶子。 However, the final return from the function is
None
and not the sum of the leaves, which is supposed to be 7
in this example.但是,该函数的最终返回值是
None
而不是叶子的总和,在本例中应该是7
。 I'd be happy to know whats wrong here.我很高兴知道这里出了什么问题。 Thank you !
谢谢 !
class Node:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
def sum_leafs(tree):
if tree is None:
return 0
if tree.right and tree.left:
sum_leafs(tree.right)
sum_leafs(tree.left)
elif tree.right or tree.left:
if tree.right:
sum_leafs(tree.right)
elif tree.left:
sum_leafs(tree.left)
elif tree.right is None and tree.left is None:
return sum_leafs(tree.left) + 1
node = Node(10)
node.right = Node(2)
node.left = Node(11)
node.left.right = Node(5)
print(sum_leafs(node))
You forgot to add +
when you sum the branches (left/right) and also you forgot to access val
which is the most crucial thing for the whole thing to work.当您对分支(左/右)求和时忘记添加
+
并且您也忘记访问val
,这是整个工作最重要的事情。
Further, the logic can be simplified:此外,逻辑可以简化:
def sum_leafs(tree):
if tree is None:
return 0
if not tree.right and not tree.left:
return tree.val
return sum_leafs(tree.right) + sum_leafs(tree.left)
You are not adding the sums together or returning them.您不是将总和加在一起或返回它们。 This can also be done with a method in the class:
这也可以通过类中的方法来完成:
class Node:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
def sum(self):
s = 0
if self.left is not None:
s += self.left.sum()
if self.right is not None:
s += self.right.sum()
return self.val + s
node = Node(10)
node.right = Node(2)
node.left = Node(11)
node.left.right = Node(5)
print(node.sum())
returns:返回:
28
You are not properly returning the calculated leaf sums.您没有正确返回计算出的叶总和。 Try this:
尝试这个:
class Node:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
def sum_leafs(tree):
if tree is None:
return 0
elif tree.right and tree.left:
return sum_leafs(tree.right) + sum_leafs(tree.left)
elif tree.right or tree.left:
if tree.right:
return sum_leafs(tree.right)
elif tree.left:
return sum_leafs(tree.left)
elif tree.right is None and tree.left is None:
return tree.val
node = Node(10)
node.right = Node(2)
node.left = Node(11)
node.left.right = Node(5)
print(sum_leafs(node))
7
node节点
First I'm going to update your Node
interface so that it's possible to set left
and right
branches when creating nodes -首先,我要更新您的
Node
接口,使得它可以设置left
和right
创建节点时的分支-
class Node:
def __init__(self, val=None, left=None, right=None):
self.left = left
self.right = right
self.val = val
This allows us to create tress more ergonomically, such as -这使我们能够更符合人体工程学地创建发辫,例如 -
t = Node(10, Node(11, None, Node(5)), Node(2))
traverse遍历
Now we write a generic traverse procedure.现在我们编写一个通用的遍历过程。 This allows us to separate 1) the traversal of our tree from 2) the intended operation we want to perform on each tree element -
这使我们能够将 1)树的遍历与 2)我们想要对每个树元素执行的预期操作分开 -
def traverse(tree):
if tree is None:
return
else:
yield tree.val
yield from traverse(tree.left)
yield from traverse(tree.right)
Now the need for sum_leafs
disappears.现在对
sum_leafs
的需求消失了。 We have decoupled traversal logic from summing logic.我们已经将遍历逻辑与求和逻辑分离。 We can calculate the sum of leafs with a simple combination of
sum
and traverse
-我们可以通过
sum
和traverse
的简单组合来计算叶子的sum
-
print(sum(traverse(t)))
# 28
don't repeat yourself不要重复自己
Or, instead of summing the values, we could write a search
function to find the first value that passes a predicate -或者,我们可以编写一个
search
函数来查找传递谓词的第一个值,而不是对这些值求和——
def search(test, tree):
for val in traverse(tree):
if test(val):
return val
print(search(lambda x: x < 10, t))
# 5
print(search(lambda x: x > 99, t))
# None
Or, we could simply collect each value into a list -或者,我们可以简单地将每个值收集到一个列表中——
print(list(traverse(t)))
# [ 10, 11, 5, 2 ]
As you can see, removing the traversal logic from each function that depends on our tree can be a huge help.如您所见,从依赖于我们的树的每个函数中删除遍历逻辑可能会有很大帮助。
without generators没有发电机
If you don't like generators, you can write the eager version of traverse
which always returns a list
.如果你不喜欢生成器,你可以编写
traverse
的急切版本,它总是返回一个list
。 The difference now is there is no way to partially traverse the tree.现在的区别是无法部分遍历树。 Note the similarities this program shares with the generator version -
请注意该程序与生成器版本的相似之处 -
def traverse(t):
if t is None:
return [] # <-- empty
else:
return \
[ t.val
, *traverse(t.left) # <-- yield from
, *traverse(t.right) # <-- yield from
]
print(traverse(t))
# [ 10, 11, 5, 2 ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.