Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Write a recursive function sumTreeNodeHelper that sums the elements of a bina

ID: 3735027 • Letter: 5

Question

5. Write a recursive function sumTreeNodeHelper that sums the elements of a binary search tree starting with the smallest element and adding elements in order up to the largest element and prints the running sum as each new value (ie. Node->val) is added to the sum (We're assuming double values are stored in the tree). (6 pts) You will not use an iterator. void sum TreeNodeHelper(struct BNode "node, double *sum) void sum Tree(struct BSTree "tree, double *sum) sum TreeNodeHelper(tree->root, sum);

Explanation / Answer

// since this is traversed in inorder
// it adds numbers in increasing order in binary search tree
void sumTreeNodeHelper(struct BNode *node, double *sum) {
    if(node != NULL) {
        sumTreeNodeHelper(node->left, sum);
        *sum += node->val;
        cout << "Sum is " << *sum << " After adding " << node->val << endl;
        sumTreeNodeHelper(node->right, sum);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote