6-liner C++ Post-order traversal


  • 0
        int maxLen = 0;
        
        int diameterOfBinaryTree(TreeNode* r) {
            return maxDown(r), maxLen;
        }
        
        int maxDown(TreeNode* x) {
            if (!x) return 0;
            int maxL = maxDown(x->left), maxR = maxDown(x->right);
            maxLen = max(maxLen, maxL + maxR);
            return max(maxL, maxR) + 1;        
        } 
    

Log in to reply
 

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