WilliamCNTH 2022-04-10 11:04 采纳率: 50%
浏览 16

二叉树递归搜索的return为什么可有可无?

BiTree ifNine(BiTree T){
    if(T != NULL){
        if(T->data == 9){
            return T;
        } else if(T->data < 9){
            return ifNine(T->rChild);
        } else if(T->data > 9){
            return ifNine(T->lChild);
        }
    }
}

 上面这段代码无论加不加return结果都是正确的,这是为什么呢?

以下是完整代码

#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>

typedef struct BiTNode {
    int data;
    struct BTnode *lChild;
    struct BTnode *rChild;
} BiTNode, *BiTree;

BiTree CreatBiTree() {
    BiTree T;
    int num;
    scanf("%d", &num);
    if (num == -1) {
        T = NULL;
    } else {
        T = (BiTNode *) malloc(sizeof(BiTNode));
        T->data = num;
        T->lChild = CreatBiTree();
        T->rChild = CreatBiTree();
    }
    return T;
}

BiTNode *InsertL(BiTNode *parent, int num) {
    BiTNode *ret;
    if (parent->lChild == NULL) {
        ret = parent->lChild = (BiTNode *) malloc(sizeof(BiTNode));
        ret->data = num;
        ret->lChild = NULL;
        ret->rChild = NULL;
        parent->lChild = ret;
        return ret;
    } else {
        BiTNode *tempP = parent->lChild;
        ret = (BiTNode *) malloc(sizeof(BiTNode));
        ret->data = num;
        ret->lChild = tempP;
        ret->rChild = NULL;
        parent->lChild = ret;
        return ret;
    }
}

BiTNode *InsertR(BiTNode *parent, int num) {
    BiTNode *ret;
    if (parent->rChild == NULL) {
        ret = parent->rChild = (BiTNode *) malloc(sizeof(BiTNode));
        ret->data = num;
        ret->lChild = NULL;
        ret->rChild = NULL;
        parent->rChild = ret;
        return ret;
    } else {
        BiTNode *tempP = parent->rChild;
        ret = (BiTNode *) malloc(sizeof(BiTNode));
        ret->data = num;
        ret->rChild = tempP;
        ret->lChild = NULL;
        parent->rChild = ret;
        return ret;
    }
}

void deleteAll(BiTNode *node) {
    if (node != NULL) {
        deleteAll(node->rChild);
        deleteAll(node->lChild);
        free(node);
    }
}

void DeleteL(BiTNode *node) {
    if (node->lChild != NULL) {
        BiTNode *tempP = node->lChild;
        deleteAll(tempP);
        node->lChild = NULL;
    }
}

void DeleteR(BiTNode *node) {
    if (node->rChild != NULL) {
        BiTNode *tempP = node->rChild;
        deleteAll(tempP);
        node->rChild = NULL;
    }
}

BiTree Search(BiTree T, int num) {

}

BiTree ifThree(BiTree T){
    if(T != NULL){
        if(T->data == 9){
            return T;
        } else if(T->data < 9){
            return ifThree(T->rChild);
        } else if(T->data > 9){
            return ifThree(T->lChild);
        }
    }
}

void TraverseQ(BiTree T) {
    if (T != NULL) {
        printf("%d", T->data);
        TraverseQ(T->lChild);
        TraverseQ(T->rChild);
    }
}

void TraverseZ(BiTree T) {
    if (T != NULL) {
        TraverseZ(T->lChild);
        printf("%d", T->data);
        TraverseZ(T->rChild);
    }
}

int main() {
    BiTree T = CreatBiTree();
    TraverseQ(T);
    printf("\n");
    TraverseZ(T);
    printf("\n");


    BiTNode *temp = ifThree(T);
    printf("%d",temp->data);

    return 0;
}
  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-04-12 13:21
    关注

    “给定一个小点的输入,完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门!
    递归函数关注以下几个因素
    ·退出条件
    ·参数有哪些
    ·返回值是什么
    ·局部变量有哪些
    ·全局变量有哪些
    ·何时输出
    ·会不会导致堆栈溢出

    评论

报告相同问题?

问题事件

  • 创建了问题 4月10日

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?