简体   繁体   English

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

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

I'm preparing for a job interview. 我正在准备面试。 I was stuck at one of the binary tree questions: 我被困在二叉树问题之一:

How can we calculate the sum of the values present in all the nodes of a binary tree? 我们如何计算二叉树所有节点中存在的值的总和?

The elegant recursive solution (in pseudo-code): 优雅的递归解决方案(伪代码):

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

then just use: 然后使用:

total = sum (root)

This correctly handles the case of a NULL root node. 这正确处理NULL根节点的情况。


And if you want to see it in action in C++, here's some code using that algorithm. 如果你想在C ++中看到它的运行,这里有一些使用该算法的代码。 First, the structure for a node and the sum function: 首先,节点的结构和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);
}

Then the code below is a test harness code for inserting nodes: 然后下面的代码是用于插入节点的测试工具代码:

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

And, finally, the main function for constructing the following tree, one that covers all of the valid possibilities (empty node, node with two children, node with no children, node with one right child and node with one left child): 最后,构建以下树的主要功能,一个涵盖所有有效可能性的树(空节点,有两个子节点的节点,没有子节点的节点,一个右子节点和一个左子节点):

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

The code to construct that tree and report the sum at various points is shown here: 构建该树并在各个点报告总和的代码如下所示:

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

This outputs (the correct): 这输出(正确):

0
10
37
149

Traverse the tree in any order (pre, post, in). 以任何顺序(pre,post,in)遍历树。 Instead of printing the node calculate the total. 而不是打印节点计算总数。

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

The same way you search the tree, or display each node, or any other tree-wide operation: visit the current node, visit the left sub-tree (recursively), and visit the right sub-tree (recursively). 搜索树,或显示每个节点或任何其他树范围操作的方式相同:访问当前节点,访问左子树(递归),并访问右子树(递归)。

Essentially, something like this: 基本上,这样的事情:

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

Because of the if checks the recursion will eventually bottom out when it reaches nodes with no left or right children (leaf nodes). 由于if检查递归最终会在到达没有左或右子节点(叶节点)的节点时触底。

While the STL has more complex and concise mechanisms for doing this, it's a very fast rode to productivity just to learn to use a manual loop over a container, something like: 虽然STL具有更复杂和简洁的机制来实现这一点,但只需学习在容器上使用手动循环就可以非常快速地提高工作效率,例如:

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

This assumes your binary tree is a STL::map, or if not you'll provide an iterator concept for your own implementation.... 假设您的二叉树是STL :: map,或者如果不是,您将为自己的实现提供迭代器概念....

Use one of the Tree Traversal techniques(In-order, Pre-order, Post-order) to visit each node and store the sum in a variable. 使用Tree Traversal技术之一(按顺序,预订,后序)访问每个节点并将总和存储在变量中。

You can find more details on tree traversal in this wiki 您可以在此Wiki中找到有关树遍历的更多详细信息

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

returns:350 收益: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