simple c++ solution


  • 0
    L

    class Solution {

    public:

    int diameterOfBinaryTree(TreeNode* root) {
        dfs(root);
        return diameter;
    }
    
    int dfs(TreeNode* root){
        if (!root) return 0;
        int left = dfs(root -> left), right = dfs(root -> right);
        diameter = max(diameter, left + right);
        return max(left, right) + 1;
    } 
    

    private:

    int diameter = 0;
    

    };


Log in to reply
 

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