Sharing my neat C++ solution


  • 0
    B
    class Solution {
    public:
        int depth(TreeNode* node) {
            if(!node) return 0;
            int ld = depth(node->left)+1;
            int rd = depth(node->right)+1;
            return max(ld,rd);
        }
        int diameterOfBinaryTree(TreeNode* root) {
            if(!root) return 0;
            if(!root->left && !root->right) return 0;
            return max(depth(root->left)+depth(root->right), max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right)));
        }
    };
    

Log in to reply
 

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