C++ short recursive solution

  • 0
    int diameterOfBinaryTree(TreeNode* root) {
        return depthDiameterOfBinaryTree(root).second;
    pair<int, int> depthDiameterOfBinaryTree(TreeNode* root) {
        if (!root) return pair<int, int> (0, 0);
        pair<int, int> left = depthDiameterOfBinaryTree(root->left);
        pair<int, int> right = depthDiameterOfBinaryTree(root->right);
        return pair<int, int> (1 + max(left.first, right.first), max(max(left.second, right.second), left.first + right.first));

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.