簡體   English   中英

二叉搜索樹中每個分支的總和

[英]Sum of Each Branch in a Binary Search Tree

我的任務是使用遞歸找到二叉搜索樹中每個分支上所有節點的總和,並將它們與用戶輸入值進行比較。 如果用戶輸入值與其中一個分支的總和相匹配,則該函數應返回 true。

二叉搜索樹

也就是說,32+24+21+14之和=91。 32+24+28+25的總和=109。 32+24+28+31=115等的總和。我嘗試了很多不同的方法,但似乎無法弄清楚如何准確遍歷每個分支。 到目前為止,我只能遍歷並找到最左邊分支的總和。

我正在使用從用戶輸入值中減去每個節點的方法。 如果葉節點處的值達到 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);
    }
}

我做錯了什么,如何修復我的代碼以找到樹上每個分支的總和? 先感謝您。 對於格式中的任何錯誤或信息缺失,我深表歉意。

這是錯誤的:

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;

如果您需要停在第一個正確的分支。

我可能會嘗試以下內容。

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);

在 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM