implementation in C


  • 1

    '''
    int depth(struct TreeNode* root){
    if (root == NULL) return 0;

    int len1,len2;
    
    len1 = 0;
    len2 = 0;
    
    if (root->left){
        len1 = depth(root->left);
    }
    
    if ( root->right){
        len2 = depth(root->right);
    }
    
    return (len1 > len2 ? len1 : len2) +1;
    

    }

    int diameterOfBinaryTree(struct TreeNode* root) {
    if ( root == NULL) return 0;

    // printf("%d\n",depth(root->left));
    // printf("%d\n",depth(root->right));
    
    int max =  depth(root->left) + depth(root->right); 
    
    int l1 = diameterOfBinaryTree(root->left);
    int l2 = diameterOfBinaryTree(root->right);
    
    
    max = max < l1 ? l1 : max;
    max = max < l2 ? l2 : max;
    return max;
    

    }

    '''


Log in to reply
 

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