如何检查树中给定路径的总和是否存在

时间:2016-10-12 15:09:33

标签: python data-structures binary-tree

我想检查从起始节点到

的任何路径中的值的总和

叶子节点存在。

例如假设我的startNode是7,而sumTarget是15,如果树是:

        a-7
  b - 1    e- 8
  c - 2    
  d -9 

然后由于7 + 8等于15,它将返回true

如果我将b作为startNode而12作为sumTotal,那么它也会返回true,因为1 + 2 + 9是以b开头的12。

class Node {
    int value;
    Node [] children
}

我认为这不对,但我不确定是什么问题。

def doesSumExist(startNode, sumTarget, currentSum):
    totalSum = sumTarget
    if startNode is not Null:
        if totalSum + startNode.value == sumTarget:
            return True
        else:
            totalSum += startNode.value
    else:
        Node startNode = doesSumExist(startNode.left, sumTarget, currentSum)  
        if startNode is not Null:
            return currentSum
        startNode = doesSumExist(startNode.right, sumTarget,currentSum)
    return False

2 个答案:

答案 0 :(得分:1)

假设您的节点类看起来像这样:

class Node:
    def __init__(self, value = 0, children = None):
        self.value = value
        self.children = [] if children is None else children

那么这种方法应该可以解决问题:

def doesSumExist(startNode, targetSum, currentSum):
    if startNode is None:
        return False
    currentSum += startNode.value
    if currentSum == targetSum:
        return True
    for child in startNode.children:
        if doesSumExist(child, targetSum, currentSum):
            return True
    return False

请注意,对于此Node类设计,startNode的None-check不是递归所必需的,而是仅用于入口点。所以这可能会更好:

def doesSumExist(startNode, targetSum):
    def inner(node, targetSum, currentSum):
        currentSum += node.value
        if currentSum == targetSum:
            return True
        #for people who like to save a few lines
        #return any(inner(child, targetSum, currentSum) for child in node.children)
        for child in node.children:
            if inner(child, targetSum, currentSum):
                return True
        return False

    if startNode is None:
        return False
    return inner(startNode, targetSum, 0)

编辑: 如果您不仅想知道总和是否存在于起始节点的路径中,而且它是否存在于任何给定的子路径中,这应该有效:

def doesSumExist(startNode, targetSum):
    def inner(node, targetSum, allValues):
        allValues.append(node.value)
        currentSum = 0
        for val in reversed(allValues):
            currentSum += val
            if currentSum == targetSum:
                return True
        for child in node.children:
            if inner(child, targetSum, allValues):
                return True
        allValues.pop()
        return False

    if startNode is None:
        return False
    return inner(startNode, targetSum, [])

答案 1 :(得分:1)

在这种情况下,我认为您正在搜索的内容如下:

def doesSumExist(startNode, sumTarget, currentSum):
    totalSum = currentSum
    if startNode is not Null:
        if totalSum + startNode.value == sumTarget: #If this node completes the sum
            return True
        else: #if not
            totalSum += startNode.value #increase current sum
    if doesSumExist(startNode.left, sumTarget, totalSum): #recursive starting on the left children
        return True
    elif doesSumExist(startNode.right, sumTarget, totalSum): #recursive starting on the right children
        return True           
    return False #if the sum is not present (starting in startNode).

但是,这不会检查节点的任何连续组合是否包含总和(代码会更复杂)。

希望这有帮助