Easy to Understand C++ Solution


  • 0
    int diameterOfBinaryTree(TreeNode* root) {
        int max = 0;
        height(root, max);
        return max;
    }
    
    int height(TreeNode* root, int & max) {
        if (root == nullptr) {
            return 0;
        }
        int lh = height(root->left, max);
        int rh = height(root->right, max);
        max = std::max(max, lh + rh);
        return std::max(lh, rh) + 1;
    }
    

Log in to reply
 

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