Can somebody explain the logic for inorder tree travesal?

48 views Asked by At

I got the output for below code but i am not getting the exact logic. Line 3(recursive fn call) will pass the value Null when it reaches leaf node , then how can it print the data part

void Inorder(struct node *node)
{
  if(node!=NULL)
  {
     Inorder(node->left);
     printf("%d",node->data);
     Inorder(node->right);
  }
}
1

There are 1 answers

0
poorvank On BEST ANSWER

Basically at every recursive call you continue if the node is not null. Hence the call node->left for the leaf node would not continue further and the nodes get printed in the next line as the recursive function unfolds.

Dry run an example and verify for yourself.