当前位置:网站首页 > 更多 > 编程开发 > 正文

[算法刷题] C语言 二叉树的最大值和高度

作者:CC下载站 日期:2022-11-08 00:00:00 浏览:70 分类:编程开发


int get_height(Node *node) {
    int left, right, max;
    if (node) {
        left = get_height(node->left);
        right = get_height(node->right);
        max = left > right ? left : right;
        return max + 1;
    } else {
        return 0;
    }
}

int get_max(Node *node) {
    if (node) {
        int ml = get_max(node->left);
        int mr = get_max(node->right);
        int root = node->data;
        int max = ml > mr ? ml : mr;
        return max > root ? max : root;
    } else {
        return -1;
    }
}
您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯