C++ Similar to tree height calculation


  • 1
    W
    class Solution {
    public:
        int helper(TreeNode* root, int & dia) {
            if (root == NULL)
                return (0);
            
            int l = helper(root->left, dia);
            int r = helper(root->right, dia);
            
            dia = max(dia, l + r);
            
            return (1 + max(l, r));
            
        }
    
        int diameterOfBinaryTree(TreeNode* root) {
            int dia = 0;
            (void) helper(root, dia);
            return (dia);
        }
    };
    

Log in to reply
 

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