繁体   English   中英

C ++:二叉树的所有节点值的总和

[英]C++: Sum of all node values of a binary tree

我正在准备面试。 我被困在二叉树问题之一:

我们如何计算二叉树所有节点中存在的值的总和?

优雅的递归解决方案(伪代码):

def sum (node):
    if node == NULL:
        return 0
    return node->value + sum (node->left) + sum (node->right)

然后使用:

total = sum (root)

这正确处理NULL根节点的情况。


如果你想在C ++中看到它的运行,这里有一些使用该算法的代码。 首先,节点的结构和sum函数:

#include <iostream>

typedef struct sNode {
    int value;
    struct sNode *left;
    struct sNode *right;
} tNode;

int sum (tNode *node) {
    if (node == 0) return 0;
    return node->value + sum (node->left) + sum (node->right);
}

然后下面的代码是用于插入节点的测试工具代码:

static tNode *addNode (tNode *parent, char leftRight, int value) {
    tNode *node = new tNode();
    node->value = value;
    node->left = 0;
    node->right = 0;

    if (parent != 0) {
        if (leftRight == 'L') {
            parent->left = node;
        } else {
            parent->right = node;
        }
    }

    return node;
}

最后,构建以下树的主要功能,一个涵盖所有有效可能性的树(空节点,有两个子节点的节点,没有子节点的节点,一个右子节点和一个左子节点):

    10
   /  \
  7    20
 /       \
3         99
 \
  4
   \
    6

构建该树并在各个点报告总和的代码如下所示:

int main (void) {
    // Empty tree first.

    tNode *root = 0;

    std::cout << sum (root) << '\n';

    // Then a tree with single node (10).

    root = addNode (0, ' ', 10);

    std::cout << sum (root) << '\n';

    // Then one with two subnodes (10, 7, 20).

    addNode (root,'L',7);
    addNode (root,'R',20);

    std::cout << sum (root) << '\n';

    // Then, finally, the full tree as per above.

    addNode (root->left,'L',3);
    addNode (root->left->left,'R',4);
    addNode (root->left->left->right,'R',6);
    addNode (root->right,'R',99);

    std::cout << sum (root) << '\n';

    return 0;
}

这输出(正确):

0
10
37
149

以任何顺序(pre,post,in)遍历树。 而不是打印节点计算总数。

void sum(Node* root, int& total)  
{   
    if(root == NULL)
    {
         return;
    }    
    sum(root->left, total);    
    total = total + root->value;   
    sum(root->right, total);  
}  
int main()  
{  
    int total =0;  
    sum(root,total);  
    cout << total;  
}

搜索树,或显示每个节点或任何其他树范围操作的方式相同:访问当前节点,访问左子树(递归),并访问右子树(递归)。

基本上,这样的事情:

int TreeNode::calculateSum() const
{
    int sum = this->value;

    if (this->left  != NULL) sum += this->left ->calculateSum();
    if (this->right != NULL) sum += this->right->calculateSum();

    return sum;
}

由于if检查递归最终会在到达没有左或右子节点(叶节点)的节点时触底。

虽然STL具有更复杂和简洁的机制来实现这一点,但只需学习在容器上使用手动循环就可以非常快速地提高工作效率,例如:

Tree::value_type total = Tree::value_type();
for (Tree::const_iterator i = tree.begin(); i != tree.end(); ++i)
    total += *i;

假设您的二叉树是STL :: map,或者如果不是,您将为自己的实现提供迭代器概念....

使用Tree Traversal技术之一(按顺序,预订,后序)访问每个节点并将总和存储在变量中。

您可以在此Wiki中找到有关树遍历的更多详细信息

  50 / \\ 30 70 / \\ / \\ 20 40 60 80 

收益:350

int Sum(struct node *root)
    {

       if(root->left == NULL && root->right== NULL)
            return root->key;

        int lvalue,rvalue;


        lvalue=Sum(root->left);
        rvalue=Sum(root->right);

        return root->key+lvalue+rvalue;

    }
public int sum(Node root){
    if(root==null){
        return 0;
    }
    if(root.left == null && root.right==null){
        return root.key;
    }
    return sum(root.left)+sum(root.right)+root.key;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM