Identifying a recursive function

做~自己de王妃 提交于 2019-12-25 07:39:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!