-2

I can in-order traverse a tree with the below code. But if want to return the in-order traversal from this function how can i do that?

void inorder(Node* root){
    if(root==NULL){
        return;
    }
    inorder(root->left);
    printf("%d\n",root->data);
    inorder(root->right);
}
red5pider
  • 351
  • 1
  • 9
  • 24

1 Answers1

-1
void inorder(Node* root,int str[])
{
     static int i=0;
     if(root==Null){
         return;
     }
    inorder(root->left,str);
    str[i++] = root->data;
    inorder(root->right,str);
    str[i] = SOME_SENTINEL_VALUE 
}
int * inorder_temp(Node *root)
{
    int *str=(int*)malloc(MAX_SIZE_OF_TREE*sizeof(int));
    inorder(root,str);
    return str;
}

I hope this is what you are asking.Call inorder_temp() in place of inorder() Edit: Sorry for not indenting properly.This is my first answer at stackoverflow

  • Please submit at least indented code. -1 for not indenting. I will remove the downvote once it is indented. And this answer is somewhat minimalist. – Jabberwocky Feb 07 '17 at 16:43