This is how the recursion works.
int size(struct node* node) // We pass root { if (node==NULL) return 0; //if the current node is null return a 0 so no add 1 is done else return(size(node->left) + 1 + size(node->right)); }
lets split up the return statement
return
size(node->left) recursive call to the left node of the current node
+1 for the current node
size(node->right) recursive call to the right node of the current node
by the time the recursion is done it will return the current size of the whole tree
if we start at the head of the tree it will call the left sub tree recursively until it hits a null, returns 0 for that node so no add 1 is done for it, then goes back up 1 level calls the right sub tree of last node not null and keeps going until the whole tree is done, adding 1 for each and only each node that is not null.
example
5 3 8 1 4 7 9
is the tree
the recursion starts at 5
it has both a left and right subtree
the left is called first 3 three has a left and right subtree left first so it goes to 1. 1 has no subtree but 1 is not null so it calls the left subtree which returns 0
0 + 1 + 0 is the 1 node return which is 1.
it then goes back up to 3
3 has a right subtree so it calls that
4 has no subtree so again its return will be 0 + 1 + 0
now back to 3 its return will be 1 + 1 + 1
now back to 5 so the return from the 3 node will be 3
so right now in the recursion it will be 3 + 1 + size->right
it will recursively return the right subtree so the return will be
3 + 1 + 3 which is 7 nodes not null