问题
As I know, a recursive function is a function which calls it self, and it has the characteristic of having a base case. This is a function for pre-order traversal of a binary tree. Is this a recursive function? Absence of the base case confuses me.
void pre_order(struct node* current){ // preorder traversal
printf("%d\n",current->data);
if(current->left != NULL){
pre_order(current->left);
}
if(current->right !=NULL){
pre_order(current->right);
}
}
回答1:
Since it calls itself it is a recursive function. That's how simple it is. There's also a base case here, but it's a little hidden perhaps. When we get to a leaf in this binary tree both left and right childs will be equal to null and therefore no more recursive calls will happen. That's our base case that's a little hidden.
来源:https://stackoverflow.com/questions/13098975/identifying-a-recursive-function