二叉搜索树中每个分支的总和

时间:2016-05-13 13:08:21

标签: c++ algorithm data-structures binary-tree binary-search-tree

我的任务是使用递归在二叉搜索树中找到每个分支上的所有节点的总和,并将它们与用户输入值进行比较。如果用户输入值与其中一个分支的总和匹配,则该函数应返回true。

binary search tree

换句话说,总和为32 + 24 + 21 + 14 = 91。总和为32 + 24 + 28 + 25 = 109。总和32 + 24 + 28 + 31 = 115等我尝试了很多不同的方法,但似乎无法弄清楚如何准确地遍历每个分支。到目前为止,我只能遍历并找到最左边分支的总和。

我正在使用从用户输入值中减去每个节点的方法。如果Leaf节点的值达到0,那么用户输入显然与树上该分支的节点和匹配。

我遇到的特殊困难点是分支发散时,例如节点[24]和[28]。我显然有一些非常简单的错误,但我无法弄明白。

以下是目前为止我编写的精简代码,采用两种配套方法(分配也需要)。

public:
bool findBranchSum1(int value) throw (InvalidTreeArgument) {
    if (root == nullptr)
        throw InvalidTreeArgument();
    return(findBranchSum(root, value));
}

private:
bool findBranchSum(NodePtr node, int value) throw (InvalidTreeArgument)
{
    bool result = false;
    if (root == nullptr)
        throw InvalidTreeArgument();

    value -= node->getElement(); //subtract current node from user-input value. 

    cout << "Current Value = " << value << endl; //help track value changes

    if (node->getLeftSide() == nullptr && node->getRightSide() == nullptr)
        {
            if (value == 0)
            {
                result = true;
                return(true);
            }
            else
                return(false);
        }
    else
    {
        if (node->getLeftSide() != nullptr)
        {
            node = node->getLeftSide(); //advance to next Left node
            result = findBranchSum(node, value); //recursive call using new node
        }
        if (node->getRightSide() != nullptr)
        {
            node = node->getRightSide(); //advance to next Right node
            result = findBranchSum(node, value); //recursive call using new node
        }
        return(result);
    }
}

我做错了什么,如何修复我的代码以找到树上每个分支的总和?先感谢您。对于格式错误或信息丢失,我道歉。

3 个答案:

答案 0 :(得分:1)

这是错误的:

if (node->getLeftSide() != nullptr)
{
  node = node->getLeftSide(); //advance to next Left node
  result = findBranchSum(node, value); //recursive call using new node
}
if (node->getRightSide() != nullptr)
{
  node = node->getRightSide(); //advance to next Right node
  result = findBranchSum(node, value); //recursive call using new node
}

因为你移动到左边然后移到左边的右边分支(node被你的任务改变了),如果它存在的话!改为:

if (node->getLeftSide() != nullptr)
{
  result = findBranchSum(node->getLeftSide(), value);
}
if (node->getRightSide() != nullptr)
{
  result = findBranchSum(node->getRightSide(), value);
}

您的退货值管理也被破坏,将其更改为:

if (node->getLeftSide() != nullptr)
{
  result = findBranchSum(node->getLeftSide(), value);
}
if (!result && node->getRightSide() != nullptr) // cut exploration if previous was correct...
{
  result = findBranchSum(node->getRightSide(), value);
}
return result;

如果你需要在第一个正确的分支处停下来。

答案 1 :(得分:0)

我可能会尝试以下内容。

bool IsLeaf(Node const * node) {
  return node && !node->left && !node->right;
}

bool CheckPathSum(Node const * node, int const target, int const sum_so_far) {
  if (!node) return false;
  int const sum = sum_so_far + node->element;
  if IsLeaf(node) && (sum == target)  return true;
  return CheckPathSum(node->left, target, sum) ||
         CheckPathSum(node->right, target, sum);
}

Call as
CheckPathSum(root, target, 0);

答案 2 :(得分:0)

在Java中,我试过这个-

private static void branchSumsUtil(TreeNode root, List<Integer> sumArray, int runningSum) {

    if (root == null){
        return;
    }

    int newRunningSum = runningSum + root.key;

    if (root.left == null && root.right == null){
        sumArray.add(newRunningSum);
    }
    branchSumsUtil(root.left, sumArray, newRunningSum);
    branchSumsUtil(root.right, sumArray, newRunningSum);
}