简体   繁体   中英

replace each node of a tree with sum of all values in left side of the node without using extra integer pointer argument

Given a binary tree, I need to change the value in each node to sum of all the values in the nodes on the left side of the node . Essentially each node should have the value equal to sum of all values of nodes visited earlier to this node in in-order traversal of the tree. Important point is this has to be done without using integer pointer argument . I am able to solve it with interger pointer argument to hold sum like this. Without this integer pointer variable, How do I hold sum when I visit right side of a node from its parent.

void modifyBST(struct node *root, int *sum) {

    if (root == NULL)  return;

    // Recur for right subtree
    modifyBSTUtil(root->left, sum);

    // Now *sum has sum of nodes in right subtree, add
    // root->data to sum and update root->data
    *sum = *sum + root->data;
    root->data = *sum;

    // Recur for left subtree
    modifyBSTUtil(root->right, sum);
}

How do I modify this method such that int *sum can be removed . My complete program is here click here

Example tree: Example tree: inorder: 4 2 5 1 6 3 7 preorder: 1 2 4 5 3 6 7 output : 4 6 11 12 18 21 28

int modify(struct node* node,int sum)
{
     if (node == NULL)
          return 0;

     int l=modify(node->left,sum);
     int r=modify(node->right,sum+l+node->data);

     int x=node->data;

     node->data=node->data+l+sum;

     return x+l+r;

}

Call the function using statement:

modify(root,0);

Full implementation : http://ideone.com/A3ezlk

One possible solution:

Rephrasing the question, what you really want to do is set each node's sum to the previous node's sum + the value associated with that node, ie

node n ->sum = node n-1 ->sum + node n ->value

Right now, you are setting node n ->sum when you visit node n , but the issue you are running into is you don't have easy access to node n-1 ->sum without passing it as a parameter. To work around this, I would suggest setting node n ->sum when you DO have easy access to node n-1 ->sum, ie when you visit node n-1

Some C code to show what I mean

void modifyBST(struct node *curNode)
{
    struct node* next = treeSuccessor(curNode);
    if(next != NULL)
    {
        next->sum = curNode->sum + next->value;
        modifyBST(next);
    }
}

The assumption is that curNode->sum has been set before modifyBST is called for that node, which is valid for the first node (as its sum is just equal to its value) and inductively valid for all other nodes.

You would use this method by finding the first node (the one with value 4 in your example), if necessary setting sum equal to its value, and calling modifyBST with that node as the argument.

TreeSuccessor is a fairly well known algorithm, you can find pseudocode for it many places online if you need.

I think below code should work just fine. I have not tested it, just checked it manually.

I hope this helps:

int modifyBST(struct node *root) {

    if (root == NULL)  return 0;

    root->data += modifyBSTUtil(root->left);

    return (root->data + modifyBSTUtil(root->right));
}

Since your pointer argument variable sum never changes, we can easily get rid of it.

int sum;

void modifyNode(struct node *root)
{
    if (!root) return;

    modifyNode(root->left);
    sum = root->data += sum;
    modifyNode(root->right);
}

void modifyTree(struct node *root)
{
    sum = 0, modifyNode(root);
}

integrated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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