【发布时间】:2014-10-21 16:52:40
【问题描述】:
Q> 给定一个二叉树,其中每个节点都有一定的权重。您必须返回二叉树中的最大权重。
最大权重 = 根节点的值 + 其左子树和右子树的值。
前 - 2
/ \
-1 3
输出 = 4
【问题讨论】:
-
不自己做作业怎么学习?
标签: java c algorithm data-structures binary-tree
Q> 给定一个二叉树,其中每个节点都有一定的权重。您必须返回二叉树中的最大权重。
最大权重 = 根节点的值 + 其左子树和右子树的值。
前 - 2
/ \
-1 3
输出 = 4
【问题讨论】:
标签: java c algorithm data-structures binary-tree
用递归很容易解决,直到没有子节点。这是一个简单的例子:
weight = getWeight(rootNode)
getWeight(node)
{
if node != null
return node.weight + getWeight(node.leftChild) + getWeight(node.rightChild)
else
return 0
}
【讨论】: