data structures - change the info part of each node in binary tree -


i have binary tree.

         2        /   \       3     4      / \     \     5  1      8      \       /      6      9 

i want change info part of each node such the

nodeinfo = nodeinfo + nextinordernodeinfo 

so actual inorder traversal

5, 6, 3, 1, 2, 4, 9, 8 

will change to

5+6,6+3,3+1,1+2,2+4,4+9,9+8,8+0   11, 9,  4,  3,  6,  13, 17, 8 

i need write function modify binary tree info parts of each node.

i have done following

calling

change(root,null); 

function definition

void change(node* n, node *k) {  if (n)    {      if (n->left) change(n->left,n);     if (n->right) change(n,n->right);     n->info + = k->info;   } }  

in way not able modify nodes right hand leaf nodes.

can give correct solution..???

thanks in advance

write reverse in-order traversal function (as in right, this, left rather left, this, right) (which technically still in-order, different definition, that's besides point).

so function process nodes in order:

8, 9, 4, 2, 1, 3, 6, 5 

this function must remember last processed node's value (before added it) , add value current node.

and here's code should work:

int last = 0; void change(node* n) {   if (n)    {     change(n->right);     int templast = n->info;     n->info += last;     last = templast;     change(n->left);   } } 

Comments