For example:
1
/ \
/ \
2 3
/ \ / \
4 5 6 7
Inorder traversal output: 4 2 5 1 6 3 7
Expected output: 4 -2 5 -1 6 -3 7
code for inorder is
Node * func(struct Node * root){
if(root!=NULL)
{
func(root->lChild);
cout<<root->nodeValue<<" ";
func(root->rChild);
}
return NULL;
}
All you might have to do , add a additional argument to keep track of alternate sign, something like following :
Node * func(struct Node * root, int& signV ){
if(root!=NULL)
{
func(root->lChild, signV);
cout<<root->nodeValue * signV <<" "; signV *= -1 ; // Change sign here
func(root->rChild, signV);
}
return NULL;
}