悟空又丢了 2020-07-16 01:35 采纳率: 0%
浏览 175
已采纳

为什么这个函数里的return无法执行?

//二叉树的输入输出操作和判断平衡
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//特殊变量声明
#define _Data char
#define _ArrayMaxSize 100

//结构体
typedef struct TreeNode{
    _Data value;
    struct TreeNode * father;
    struct TreeNode * right;
    struct TeenNode * left;
}* pNode, Node;

pNode createNode(_Data value) {
    pNode node = (pNode) malloc(sizeof(Node));

    node->father = NULL;
    node->left = NULL;
    node->right = NULL;
    node->value = value;

    return node;
}

void createByLeft(pNode father) {
    _Data value;
    scanf("%c", &value);
    if (value != '#') {
        pNode left = createNode(value);
        father->left = left;
        createByLeft(left);
    }

    scanf("%c", &value);
    if (value != '#') {
        pNode right = createNode(value);
        father->right = right;
        createByLeft(right);
    }
}

void destoryTree(pNode father) {
    if (father->right != NULL) {
        destoryTree(father->right);
    }
    if (father->left != NULL) {
        destoryTree(father->left);
    }

    free(father);
}

int getHigh(pNode father) {
    int left = 1, right = 1;
    if (father->left != NULL) {
        left += getHigh(father->left);
    }

    if (father->right != NULL) {
        right += getHigh(father->right);
    }

    return left>right?left:right;
}

void printTree(pNode father) {
    pNode res[_ArrayMaxSize];   //存储列队
    int f = 0, r = 0;   //前指针 后指针
    res[f++] = father;
    int count = 0;
    int heigh = getHigh(father) + 1;
    while(r != f){
//        树循环
        pNode cRes[_ArrayMaxSize];   //打印队列
        int cf = 0, cr = 0;
        while(r != f){
            cRes[cf++] = res[(r++)%_ArrayMaxSize];
        }

        int spaceS = (1 << (heigh - count))+ (heigh - count) - 1;
        for(int i = 0; i < spaceS; i++) printf(" ");

        int flag = 1;
        while(cr != cf){

            //层循环
            pNode temp = cRes[cr++];
            if(temp != NULL){
                res[(f++)%_ArrayMaxSize] = temp->left;
                res[(f++)%_ArrayMaxSize] = temp->right;
                printf("%c", temp->value);
            } else{
                printf("#");
            }
            if(flag > 0){
                for(int j = (1 << (heigh - count + 1)); j >= 0; j--) printf(" ");
            }else{
                for(int j = (1 << (heigh - count + 1)); j > 1; j--) printf(" ");
            }
            flag *= -1;

        }
        printf("\n");
        count++;
    }

}

void printByFirst(pNode father){
    if(father != NULL){
        printf("%c", father->value);
        if(father->left != NULL){
            printByFirst(father->left);
        }
        if(father->right != NULL){
            printByFirst(father->right);
        }
    }
}

void printByCenter(pNode father){
    if(father != NULL){
        if(father->left != NULL){
            printByFirst(father->left);
        }
        printf("%c", father->value);
        if(father->right != NULL){
            printByFirst(father->right);
        }
    }

}

void printByEnd(pNode father){
    if(father != NULL){
        if(father->left != NULL){
            printByFirst(father->left);
        }
        if(father->right != NULL){
            printByFirst(father->right);
        }
        printf("%c", father->value);
    }
}


int depth(struct TreeNode *root) 
{
    if(root == NULL)
    {
        return 0;
    }
    int dep1=depth(root->left);
    int dep2=depth(root->right);
    return (dep1>dep2?dep1:dep2)+1;
}

int isBalanced(struct TreeNode* root){
    if(root == NULL)
    {
        return 0;
    }
    int c = abs(depth(root->left) - depth(root->right));
    printf("%d\t", depth(root->right)); printf("%d\t", depth(root->left));
    printf("%d\n", c);

    if(c >= 2) 
    {
        return 1;
    }
    isBalanced(root->right);
    isBalanced(root->left);

    return 0;
}



//测试
int main() {
    pNode node  = createNode('A');

    createByLeft(node);

    printf("树的深度:%d\n", getHigh(node));
    printf("先序输出:");
    printByFirst(node);
    printf("\n中序输出:");
    printByCenter(node);
    printf("\n后序输出:");
    printByEnd(node);

    printf("\n");

    if(isBalanced(node) == 1) printf("false\n"); else printf("true\n");


    destoryTree(node);
}

测试结果为
图片说明

在isBalanceed函数中,可以看出c=2时,if(c>=2)是成立的,为什么没有return 1呢?
return之后的语句应该不会执行的吧。这里好像执行return 1后继续执行后续代码

图片中第三列是c的值。

希望各位指点。

  • 写回答

1条回答 默认 最新

  • wlj1234 2020-07-17 14:11
    关注

    isBalanced你只返回第一层调用的结果
    isBalanced(root->right);
    isBalanced(root->left);
    这两句相当于没有执行,调整参考下面

    bool isLeftBalanced = false;
    bool isRightBalanced = false;
    
    if(isBalanced(root->right) == 1)
        isLeftBalanced = true;
    if(isBalanced(root->left) == 1)
        isRightBalanced = true;
    
    if(isLeftBalanced && isRightBalanced)
        return 1;
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 7月16日

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)