自学内容网 自学内容网

C语言 | Leetcode C语言题解之第543题二叉树的直径

题目:

题解:

typedef struct TreeNode Node;

int method (Node* root, int* max) {
        if (root == NULL) 
                return 0;

        int left = method (root->left, max);
        int right = method (root->right, max);
        
        *max = *max > (left + right) ? *max : (left + right);

        return (left > right ? left : right) + 1;
}

int diameterOfBinaryTree(struct TreeNode* root) {
        int max = 0;
        method (root, &max);
        return max;
}

原文地址:https://blog.csdn.net/m0_59237910/article/details/143583796

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!