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;
}
上一篇:C++的智能指针