i have a binary tree.
2 / \ 3 4 / \ \ 5 1 8 \ / 6 9
I want to change the info part of each node such that the
nodeinfo = nodeinfo + nextInorderNodeInfo
so the 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 to write a function that will modify the binary tree info parts of each node.
i have done the 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 this way i am not able to modify the nodes that are right hand leaf nodes.
can someone give the correct solution..???
thanks in advance
Asked By : Koka
Answered By : wece
In my point of view, since you want to use the next info in the inorder traversal, you have to do a function that traverse you tree backward for this order.
int change(node* n,int next){ int k; k=next; if(n){ if(n->right) k = change(n->right,next); if(n->left) change(n->left,n->info); n->info += k; } }
That way, calling
change(root,0)
You have next that is the info of the next node in your order.
I hope I didn't made a mistake and that it help.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/11825
0 comments:
Post a Comment
Let us know your responses and feedback